blob: d6a8dddb2268bab8a341499d0debadba041f3928 [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(
134 struct inode *ip, /* inode */
135 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
142 mapping = ip->i_mapping;
143 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(
392 struct inode *ip,
393 xfs_iocore_t *io,
Lachlan McIlroy68160162007-02-10 18:36:47 +1100394 xfs_fsize_t offset,
395 xfs_fsize_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 xfs_fileoff_t last_fsb;
Nathan Scott1b895842006-03-31 13:08:59 +1000398 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int nimaps;
400 int zero_offset;
401 int zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int error = 0;
403 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Eric Sandeen24ee8082006-01-11 15:34:32 +1100407 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
408 if (zero_offset == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * There are no extra bytes in the last block on disk to
411 * zero, so return.
412 */
413 return 0;
414 }
415
416 last_fsb = XFS_B_TO_FSBT(mp, isize);
417 nimaps = 1;
418 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000419 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (error) {
421 return error;
422 }
423 ASSERT(nimaps > 0);
424 /*
425 * If the block underlying isize is just a hole, then there
426 * is nothing to zero.
427 */
428 if (imap.br_startblock == HOLESTARTBLOCK) {
429 return 0;
430 }
431 /*
432 * Zero the part of the last block beyond the EOF, and write it
433 * out sync. We need to drop the ilock while we do this so we
434 * don't deadlock when the buffer cache calls back to us.
435 */
436 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
Nathan Scott1b895842006-03-31 13:08:59 +1000437
Eric Sandeen24ee8082006-01-11 15:34:32 +1100438 zero_len = mp->m_sb.sb_blocksize - zero_offset;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100439 if (isize + zero_len > offset)
440 zero_len = offset - isize;
441 error = xfs_iozero(ip, isize, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
444 ASSERT(error >= 0);
445 return error;
446}
447
448/*
449 * Zero any on disk space between the current EOF and the new,
450 * larger EOF. This handles the normal case of zeroing the remainder
451 * of the last block in the file and the unusual case of zeroing blocks
452 * out beyond the size of the file. This second case only happens
453 * with fixed size extents and when the system crashes before the inode
454 * size was updated but after blocks were allocated. If fill is set,
455 * then any holes in the range are filled and zeroed. If not, the holes
456 * are left alone as holes.
457 */
458
459int /* error (positive) */
460xfs_zero_eof(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000461 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 xfs_iocore_t *io,
463 xfs_off_t offset, /* starting I/O offset */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100464 xfs_fsize_t isize) /* current inode size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Nathan Scottec86dc02006-03-17 17:25:36 +1100466 struct inode *ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 xfs_fileoff_t start_zero_fsb;
468 xfs_fileoff_t end_zero_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfs_fileoff_t zero_count_fsb;
470 xfs_fileoff_t last_fsb;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100471 xfs_fileoff_t zero_off;
472 xfs_fsize_t zero_len;
Nathan Scott1b895842006-03-31 13:08:59 +1000473 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int nimaps;
475 int error = 0;
476 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
479 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
Eric Sandeen24ee8082006-01-11 15:34:32 +1100480 ASSERT(offset > isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /*
483 * First handle zeroing the block on which isize resides.
484 * We only zero a part of that block so it is handled specially.
485 */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100486 error = xfs_zero_last_block(ip, io, offset, isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (error) {
488 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
489 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
490 return error;
491 }
492
493 /*
494 * Calculate the range between the new size and the old
495 * where blocks needing to be zeroed may exist. To get the
496 * block where the last byte in the file currently resides,
497 * we need to subtract one from the size and truncate back
498 * to a block boundary. We subtract 1 in case the size is
499 * exactly on a block boundary.
500 */
501 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
502 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
503 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
504 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
505 if (last_fsb == end_zero_fsb) {
506 /*
507 * The size was only incremented on its last block.
508 * We took care of that above, so just return.
509 */
510 return 0;
511 }
512
513 ASSERT(start_zero_fsb <= end_zero_fsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 while (start_zero_fsb <= end_zero_fsb) {
515 nimaps = 1;
516 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
517 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000518 0, NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (error) {
520 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
521 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
522 return error;
523 }
524 ASSERT(nimaps > 0);
525
526 if (imap.br_state == XFS_EXT_UNWRITTEN ||
527 imap.br_startblock == HOLESTARTBLOCK) {
528 /*
529 * This loop handles initializing pages that were
530 * partially initialized by the code below this
531 * loop. It basically zeroes the part of the page
532 * that sits on a hole and sets the page as P_HOLE
533 * and calls remapf if it is a mapped file.
534 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100535 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
537 continue;
538 }
539
540 /*
Nathan Scott3d80ede2006-06-09 14:57:30 +1000541 * There are blocks we need to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * Drop the inode lock while we're doing the I/O.
543 * We'll still have the iolock to protect us.
544 */
545 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
546
Lachlan McIlroy68160162007-02-10 18:36:47 +1100547 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
548 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
549
550 if ((zero_off + zero_len) > offset)
551 zero_len = offset - zero_off;
552
553 error = xfs_iozero(ip, zero_off, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (error) {
555 goto out_lock;
556 }
557
Nathan Scott3d80ede2006-06-09 14:57:30 +1000558 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
560
561 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
562 }
563
564 return 0;
565
566out_lock:
567
568 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
569 ASSERT(error >= 0);
570 return error;
571}
572
573ssize_t /* bytes written, or (-) error */
574xfs_write(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000575 struct xfs_inode *xip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct kiocb *iocb,
577 const struct iovec *iovp,
578 unsigned int nsegs,
579 loff_t *offset,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000580 int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct file *file = iocb->ki_filp;
583 struct address_space *mapping = file->f_mapping;
584 struct inode *inode = mapping->host;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000585 bhv_vnode_t *vp = XFS_ITOV(xip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 unsigned long segs = nsegs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 xfs_mount_t *mp;
588 ssize_t ret = 0, error = 0;
589 xfs_fsize_t isize, new_size;
590 xfs_iocore_t *io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int iolock;
592 int eventsent = 0;
Nathan Scott8285fb52006-06-09 17:07:12 +1000593 bhv_vrwlock_t locktype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 size_t ocount = 0, count;
595 loff_t pos;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000596 int need_i_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 XFS_STATS_INC(xs_write_calls);
599
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -0700600 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
601 if (error)
602 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 count = ocount;
605 pos = *offset;
606
607 if (count == 0)
608 return 0;
609
610 io = &xip->i_iocore;
611 mp = io->io_mount;
612
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000613 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
Nathan Scott34327e12006-06-09 17:11:55 +1000614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (XFS_FORCED_SHUTDOWN(mp))
616 return -EIO;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618relock:
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000619 if (ioflags & IO_ISDIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 iolock = XFS_IOLOCK_SHARED;
621 locktype = VRWLOCK_WRITE_DIRECT;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000622 need_i_mutex = 0;
623 } else {
624 iolock = XFS_IOLOCK_EXCL;
625 locktype = VRWLOCK_WRITE;
626 need_i_mutex = 1;
627 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632start:
633 error = -generic_write_checks(file, &pos, &count,
634 S_ISBLK(inode->i_mode));
635 if (error) {
636 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100637 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000640 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 !(ioflags & IO_INVIS) && !eventsent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int dmflags = FILP_DELAY_FLAG(file);
643
Nathan Scotta13828b2006-03-17 17:26:14 +1100644 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800645 dmflags |= DM_FLAGS_IMUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 xfs_iunlock(xip, XFS_ILOCK_EXCL);
648 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
649 pos, count,
650 dmflags, &locktype);
651 if (error) {
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000652 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 xfs_ilock(xip, XFS_ILOCK_EXCL);
655 eventsent = 1;
656
657 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000658 * The iolock was dropped and reacquired in XFS_SEND_DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * so we have to recheck the size when appending.
660 * We will only "goto start;" once, since having sent the
661 * event prevents another call to XFS_SEND_DATA, which is
662 * what allows the size to change in the first place.
663 */
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000664 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
665 goto start;
666 }
667
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000668 if (ioflags & IO_ISDIRECT) {
669 xfs_buftarg_t *target =
670 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
671 mp->m_rtdev_targp : mp->m_ddev_targp;
672
673 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
674 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
675 return XFS_ERROR(-EINVAL);
676 }
677
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000678 if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000679 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
680 iolock = XFS_IOLOCK_EXCL;
681 locktype = VRWLOCK_WRITE;
682 need_i_mutex = 1;
683 mutex_lock(&inode->i_mutex);
684 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto start;
686 }
687 }
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 new_size = pos + count;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000690 if (new_size > xip->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 io->io_new_size = new_size;
692
Nathan Scott4aeb6642005-11-02 11:43:58 +1100693 if (likely(!(ioflags & IO_INVIS))) {
Christoph Hellwig870f4812006-01-09 20:52:01 -0800694 file_update_time(file);
Nathan Scott4aeb6642005-11-02 11:43:58 +1100695 xfs_ichgtime_fast(xip, inode,
696 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 /*
700 * If the offset is beyond the size of the file, we have a couple
701 * of things to do. First, if there is already space allocated
702 * we need to either create holes or zero the disk or ...
703 *
704 * If there is a page where the previous size lands, we need
705 * to zero it out up to the new size.
706 */
707
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000708 if (pos > xip->i_size) {
Christoph Hellwig993386c2007-08-28 16:12:30 +1000709 error = xfs_zero_eof(vp, io, pos, xip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (error) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000711 xfs_iunlock(xip, XFS_ILOCK_EXCL);
712 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 }
715 xfs_iunlock(xip, XFS_ILOCK_EXCL);
716
717 /*
718 * If we're writing the file then make sure to clear the
719 * setuid and setgid bits if the process is not being run
720 * by root. This keeps people from modifying setuid and
721 * setgid binaries.
722 */
723
724 if (((xip->i_d.di_mode & S_ISUID) ||
725 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
726 (S_ISGID | S_IXGRP))) &&
727 !capable(CAP_FSETID)) {
728 error = xfs_write_clear_setuid(xip);
729 if (likely(!error))
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800730 error = -remove_suid(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (unlikely(error)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000732 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
735
736retry:
737 /* We can write back this queue in page reclaim */
738 current->backing_dev_info = mapping->backing_dev_info;
739
740 if ((ioflags & IO_ISDIRECT)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000741 if (VN_CACHED(vp)) {
742 WARN_ON(need_i_mutex == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 xfs_inval_cached_trace(io, pos, -1,
744 ctooff(offtoct(pos)), -1);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000745 error = xfs_flushinval_pages(xip,
746 ctooff(offtoct(pos)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000748 if (error)
749 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Nathan Scotta13828b2006-03-17 17:26:14 +1100752 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /* demote the lock now the cached pages are gone */
754 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800755 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 iolock = XFS_IOLOCK_SHARED;
758 locktype = VRWLOCK_WRITE_DIRECT;
Nathan Scotta13828b2006-03-17 17:26:14 +1100759 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
762 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
763 *offset, ioflags);
764 ret = generic_file_direct_write(iocb, iovp,
765 &segs, pos, offset, count, ocount);
766
767 /*
768 * direct-io write to a hole: fall through to buffered I/O
769 * for completing the rest of the request.
770 */
771 if (ret >= 0 && ret != count) {
772 XFS_STATS_ADD(xs_write_bytes, ret);
773
774 pos += ret;
775 count -= ret;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ioflags &= ~IO_ISDIRECT;
778 xfs_iunlock(xip, iolock);
779 goto relock;
780 }
781 } else {
782 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
783 *offset, ioflags);
784 ret = generic_file_buffered_write(iocb, iovp, segs,
785 pos, offset, count, ret);
786 }
787
788 current->backing_dev_info = NULL;
789
790 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
791 ret = wait_on_sync_kiocb(iocb);
792
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000793 if (ret == -ENOSPC &&
794 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
Christoph Hellwig993386c2007-08-28 16:12:30 +1000795 xfs_rwunlock(xip, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100796 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800797 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
799 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
800 0, 0, 0); /* Delay flag intentionally unused */
Nathan Scotta13828b2006-03-17 17:26:14 +1100801 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800802 mutex_lock(&inode->i_mutex);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000803 xfs_rwlock(xip, locktype);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000804 if (error)
805 goto out_unlock_internal;
806 pos = xip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ret = 0;
808 goto retry;
809 }
810
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100811 isize = i_size_read(inode);
812 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
813 *offset = isize;
814
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000815 if (*offset > xip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 xfs_ilock(xip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000817 if (*offset > xip->i_size)
818 xip->i_size = *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 xfs_iunlock(xip, XFS_ILOCK_EXCL);
820 }
821
822 error = -ret;
823 if (ret <= 0)
824 goto out_unlock_internal;
825
826 XFS_STATS_ADD(xs_write_bytes, ret);
827
828 /* Handle various SYNC-type writes */
829 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000830 int error2;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000831 xfs_rwunlock(xip, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100832 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800833 mutex_unlock(&inode->i_mutex);
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000834 error2 = sync_page_range(inode, mapping, pos, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!error)
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000836 error = error2;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000837 if (need_i_mutex)
838 mutex_lock(&inode->i_mutex);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000839 xfs_rwlock(xip, locktype);
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000840 error2 = xfs_write_sync_logforce(mp, xip);
841 if (!error)
842 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
845 out_unlock_internal:
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000846 if (io->io_new_size) {
847 xfs_ilock(xip, XFS_ILOCK_EXCL);
848 io->io_new_size = 0;
849 /*
850 * If this was a direct or synchronous I/O that failed (such
851 * as ENOSPC) then part of the I/O may have been written to
852 * disk before the error occured. In this case the on-disk
853 * file size may have been adjusted beyond the in-memory file
854 * size and now needs to be truncated back.
855 */
856 if (xip->i_d.di_size > xip->i_size)
857 xip->i_d.di_size = xip->i_size;
858 xfs_iunlock(xip, XFS_ILOCK_EXCL);
859 }
Christoph Hellwig993386c2007-08-28 16:12:30 +1000860 xfs_rwunlock(xip, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100861 out_unlock_mutex:
862 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800863 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return -error;
865}
866
867/*
868 * All xfs metadata buffers except log state machine buffers
869 * get this attached as their b_bdstrat callback function.
870 * This is so that we can catch a buffer
871 * after prematurely unpinning it to forcibly shutdown the filesystem.
872 */
873int
874xfs_bdstrat_cb(struct xfs_buf *bp)
875{
876 xfs_mount_t *mp;
877
878 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
879 if (!XFS_FORCED_SHUTDOWN(mp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100880 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return 0;
882 } else {
883 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
884 /*
885 * Metadata write that didn't get logged but
886 * written delayed anyway. These aren't associated
887 * with a transaction, and can be ignored.
888 */
889 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
890 (XFS_BUF_ISREAD(bp)) == 0)
891 return (xfs_bioerror_relse(bp));
892 else
893 return (xfs_bioerror(bp));
894 }
895}
896
897
898int
Christoph Hellwig993386c2007-08-28 16:12:30 +1000899xfs_bmap(
900 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 xfs_off_t offset,
902 ssize_t count,
903 int flags,
904 xfs_iomap_t *iomapp,
905 int *niomaps)
906{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 xfs_iocore_t *io = &ip->i_iocore;
908
909 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
910 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
911 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
912
913 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
914}
915
916/*
917 * Wrapper around bdstrat so that we can stop data
918 * from going to disk in case we are shutting down the filesystem.
919 * Typically user data goes thru this path; one of the exceptions
920 * is the superblock.
921 */
922int
923xfsbdstrat(
924 struct xfs_mount *mp,
925 struct xfs_buf *bp)
926{
927 ASSERT(mp);
928 if (!XFS_FORCED_SHUTDOWN(mp)) {
929 /* Grio redirection would go here
930 * if (XFS_BUF_IS_GRIO(bp)) {
931 */
932
Nathan Scottce8e9222006-01-11 15:39:08 +1100933 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935 }
936
937 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
938 return (xfs_bioerror_relse(bp));
939}
940
941/*
942 * If the underlying (data/log/rt) device is readonly, there are some
943 * operations that cannot proceed.
944 */
945int
946xfs_dev_is_read_only(
947 xfs_mount_t *mp,
948 char *message)
949{
950 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
951 xfs_readonly_buftarg(mp->m_logdev_targp) ||
952 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
953 cmn_err(CE_NOTE,
954 "XFS: %s required on read-only device.", message);
955 cmn_err(CE_NOTE,
956 "XFS: write access unavailable, cannot proceed.");
957 return EROFS;
958 }
959 return 0;
960}