blob: 6c3a846a5267b6364d774a4ce9c5c945a9e448b5 [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,
Christoph Hellwig613d7042007-10-11 17:44:08 +100061 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 void *data,
63 size_t segs,
64 loff_t offset,
65 int ioflags)
66{
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (ip->i_rwtrace == NULL)
68 return;
69 ktrace_enter(ip->i_rwtrace,
70 (void *)(unsigned long)tag,
71 (void *)ip,
72 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
73 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
74 (void *)data,
75 (void *)((unsigned long)segs),
76 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
77 (void *)((unsigned long)(offset & 0xffffffff)),
78 (void *)((unsigned long)ioflags),
Christoph Hellwig613d7042007-10-11 17:44:08 +100079 (void *)((unsigned long)((ip->i_new_size >> 32) & 0xffffffff)),
80 (void *)((unsigned long)(ip->i_new_size & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +110081 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 (void *)NULL,
83 (void *)NULL,
84 (void *)NULL,
85 (void *)NULL);
86}
87
88void
89xfs_inval_cached_trace(
Christoph Hellwig613d7042007-10-11 17:44:08 +100090 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 xfs_off_t offset,
92 xfs_off_t len,
93 xfs_off_t first,
94 xfs_off_t last)
95{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if (ip->i_rwtrace == NULL)
98 return;
99 ktrace_enter(ip->i_rwtrace,
100 (void *)(__psint_t)XFS_INVAL_CACHED,
101 (void *)ip,
102 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
103 (void *)((unsigned long)(offset & 0xffffffff)),
104 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(len & 0xffffffff)),
106 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
107 (void *)((unsigned long)(first & 0xffffffff)),
108 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
109 (void *)((unsigned long)(last & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +1100110 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 (void *)NULL,
112 (void *)NULL,
113 (void *)NULL,
114 (void *)NULL,
115 (void *)NULL);
116}
117#endif
118
119/*
120 * xfs_iozero
121 *
122 * xfs_iozero clears the specified range of buffer supplied,
123 * and marks all the affected blocks as valid and modified. If
124 * an affected block is not allocated, it will be allocated. If
125 * an affected block is not completely overwritten, and is not
126 * valid before the operation, it will be read from disk before
127 * being partially zeroed.
128 */
129STATIC int
130xfs_iozero(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000131 struct xfs_inode *ip, /* inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 loff_t pos, /* offset in file */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100133 size_t count) /* size of data to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct page *page;
136 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int status;
138
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000139 mapping = ip->i_vnode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 do {
Nick Piggind79689c2007-10-16 01:25:06 -0700141 unsigned offset, bytes;
142 void *fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 bytes = PAGE_CACHE_SIZE - offset;
146 if (bytes > count)
147 bytes = count;
148
Nick Piggind79689c2007-10-16 01:25:06 -0700149 status = pagecache_write_begin(NULL, mapping, pos, bytes,
150 AOP_FLAG_UNINTERRUPTIBLE,
151 &page, &fsdata);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100152 if (status)
Nick Piggind79689c2007-10-16 01:25:06 -0700153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Christoph Hellwig700716c2007-05-24 15:27:17 +1000155 zero_user_page(page, offset, bytes, KM_USER0);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100156
Nick Piggind79689c2007-10-16 01:25:06 -0700157 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
158 page, fsdata);
159 WARN_ON(status <= 0); /* can't return less than zero! */
160 pos += bytes;
161 count -= bytes;
162 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 } while (count);
164
165 return (-status);
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168ssize_t /* bytes read, or (-) error */
169xfs_read(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000170 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct kiocb *iocb,
172 const struct iovec *iovp,
173 unsigned int segs,
174 loff_t *offset,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000175 int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct file *file = iocb->ki_filp;
178 struct inode *inode = file->f_mapping->host;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000179 bhv_vnode_t *vp = XFS_ITOV(ip);
180 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 size_t size = 0;
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000182 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 xfs_fsize_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 unsigned long seg;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 XFS_STATS_INC(xs_read_calls);
188
189 /* START copy & waste from filemap.c */
190 for (seg = 0; seg < segs; seg++) {
191 const struct iovec *iv = &iovp[seg];
192
193 /*
194 * If any segment has a negative length, or the cumulative
195 * length ever wraps negative then return -EINVAL.
196 */
197 size += iv->iov_len;
198 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
199 return XFS_ERROR(-EINVAL);
200 }
201 /* END copy & waste from filemap.c */
202
203 if (unlikely(ioflags & IO_ISDIRECT)) {
204 xfs_buftarg_t *target =
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100205 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 mp->m_rtdev_targp : mp->m_ddev_targp;
Nathan Scottce8e9222006-01-11 15:39:08 +1100207 if ((*offset & target->bt_smask) ||
208 (size & target->bt_smask)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000209 if (*offset == ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return (0);
211 }
212 return -XFS_ERROR(EINVAL);
213 }
214 }
215
216 n = XFS_MAXIOFFSET(mp) - *offset;
217 if ((n <= 0) || (size == 0))
218 return 0;
219
220 if (n < size)
221 size = n;
222
Nathan Scotta13828b2006-03-17 17:26:14 +1100223 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (unlikely(ioflags & IO_ISDIRECT))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800227 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 xfs_ilock(ip, XFS_IOLOCK_SHARED);
229
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000230 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000231 bhv_vrwlock_t locktype = VRWLOCK_READ;
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000232 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Christoph Hellwig993386c2007-08-28 16:12:30 +1000234 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *offset, size,
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000235 dmflags, &locktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (ret) {
237 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000238 if (unlikely(ioflags & IO_ISDIRECT))
239 mutex_unlock(&inode->i_mutex);
240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 }
243
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000244 if (unlikely(ioflags & IO_ISDIRECT)) {
245 if (VN_CACHED(vp))
Tim Shimmine6a4b372007-11-23 16:30:42 +1100246 ret = xfs_flushinval_pages(ip, (*offset & PAGE_CACHE_MASK),
247 -1, FI_REMAPF_LOCKED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000248 mutex_unlock(&inode->i_mutex);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000249 if (ret) {
250 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
251 return ret;
252 }
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000253 }
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000254
Christoph Hellwig613d7042007-10-11 17:44:08 +1000255 xfs_rw_enter_trace(XFS_READ_ENTER, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 (void *)iovp, segs, *offset, ioflags);
Badari Pulavarty543ade12006-09-30 23:28:48 -0700257
258 iocb->ki_pos = *offset;
259 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
261 ret = wait_on_sync_kiocb(iocb);
262 if (ret > 0)
263 XFS_STATS_ADD(xs_read_bytes, ret);
264
265 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return ret;
267}
268
269ssize_t
Nathan Scott1b895842006-03-31 13:08:59 +1000270xfs_splice_read(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000271 xfs_inode_t *ip,
Nathan Scott1b895842006-03-31 13:08:59 +1000272 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200273 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200274 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000275 size_t count,
276 int flags,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000277 int ioflags)
Nathan Scott1b895842006-03-31 13:08:59 +1000278{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000279 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott1b895842006-03-31 13:08:59 +1000280 xfs_mount_t *mp = ip->i_mount;
281 ssize_t ret;
282
283 XFS_STATS_INC(xs_read_calls);
284 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
285 return -EIO;
286
287 xfs_ilock(ip, XFS_IOLOCK_SHARED);
288
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000289 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000290 bhv_vrwlock_t locktype = VRWLOCK_READ;
Nathan Scott1b895842006-03-31 13:08:59 +1000291 int error;
292
Christoph Hellwig993386c2007-08-28 16:12:30 +1000293 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000294 FILP_DELAY_FLAG(infilp), &locktype);
295 if (error) {
296 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
297 return -error;
298 }
299 }
Christoph Hellwig613d7042007-10-11 17:44:08 +1000300 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, ip,
Jens Axboecbb7e572006-04-11 14:57:50 +0200301 pipe, count, *ppos, ioflags);
302 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000303 if (ret > 0)
304 XFS_STATS_ADD(xs_read_bytes, ret);
305
306 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
307 return ret;
308}
309
310ssize_t
311xfs_splice_write(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000312 xfs_inode_t *ip,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200313 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000314 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200315 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000316 size_t count,
317 int flags,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000318 int ioflags)
Nathan Scott1b895842006-03-31 13:08:59 +1000319{
Christoph Hellwig993386c2007-08-28 16:12:30 +1000320 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott1b895842006-03-31 13:08:59 +1000321 xfs_mount_t *mp = ip->i_mount;
322 ssize_t ret;
David Chinner0a8d17d2006-09-07 14:27:15 +1000323 struct inode *inode = outfilp->f_mapping->host;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000324 xfs_fsize_t isize, new_size;
Nathan Scott1b895842006-03-31 13:08:59 +1000325
326 XFS_STATS_INC(xs_write_calls);
327 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
328 return -EIO;
329
330 xfs_ilock(ip, XFS_IOLOCK_EXCL);
331
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000332 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000333 bhv_vrwlock_t locktype = VRWLOCK_WRITE;
Nathan Scott1b895842006-03-31 13:08:59 +1000334 int error;
335
Christoph Hellwig993386c2007-08-28 16:12:30 +1000336 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp, *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000337 FILP_DELAY_FLAG(outfilp), &locktype);
338 if (error) {
339 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
340 return -error;
341 }
342 }
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000343
344 new_size = *ppos + count;
345
346 xfs_ilock(ip, XFS_ILOCK_EXCL);
347 if (new_size > ip->i_size)
Christoph Hellwig613d7042007-10-11 17:44:08 +1000348 ip->i_new_size = new_size;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000349 xfs_iunlock(ip, XFS_ILOCK_EXCL);
350
Christoph Hellwig613d7042007-10-11 17:44:08 +1000351 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, ip,
Jens Axboecbb7e572006-04-11 14:57:50 +0200352 pipe, count, *ppos, ioflags);
353 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000354 if (ret > 0)
355 XFS_STATS_ADD(xs_write_bytes, ret);
356
David Chinner0a8d17d2006-09-07 14:27:15 +1000357 isize = i_size_read(inode);
358 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
359 *ppos = isize;
360
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000361 if (*ppos > ip->i_size) {
David Chinner0a8d17d2006-09-07 14:27:15 +1000362 xfs_ilock(ip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000363 if (*ppos > ip->i_size)
364 ip->i_size = *ppos;
365 xfs_iunlock(ip, XFS_ILOCK_EXCL);
366 }
367
Christoph Hellwig613d7042007-10-11 17:44:08 +1000368 if (ip->i_new_size) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000369 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig613d7042007-10-11 17:44:08 +1000370 ip->i_new_size = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000371 if (ip->i_d.di_size > ip->i_size)
372 ip->i_d.di_size = ip->i_size;
David Chinner0a8d17d2006-09-07 14:27:15 +1000373 xfs_iunlock(ip, XFS_ILOCK_EXCL);
374 }
Nathan Scott1b895842006-03-31 13:08:59 +1000375 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return ret;
377}
378
379/*
380 * This routine is called to handle zeroing any space in the last
381 * block of the file that is beyond the EOF. We do this since the
382 * size is being increased without writing anything to that block
383 * and we don't want anyone to read the garbage on the disk.
384 */
385STATIC int /* error (positive) */
386xfs_zero_last_block(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000387 xfs_inode_t *ip,
Lachlan McIlroy68160162007-02-10 18:36:47 +1100388 xfs_fsize_t offset,
389 xfs_fsize_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 xfs_fileoff_t last_fsb;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000392 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int nimaps;
394 int zero_offset;
395 int zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int error = 0;
397 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000399 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Eric Sandeen24ee8082006-01-11 15:34:32 +1100401 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
402 if (zero_offset == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /*
404 * There are no extra bytes in the last block on disk to
405 * zero, so return.
406 */
407 return 0;
408 }
409
410 last_fsb = XFS_B_TO_FSBT(mp, isize);
411 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000412 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000413 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (error) {
415 return error;
416 }
417 ASSERT(nimaps > 0);
418 /*
419 * If the block underlying isize is just a hole, then there
420 * is nothing to zero.
421 */
422 if (imap.br_startblock == HOLESTARTBLOCK) {
423 return 0;
424 }
425 /*
426 * Zero the part of the last block beyond the EOF, and write it
427 * out sync. We need to drop the ilock while we do this so we
428 * don't deadlock when the buffer cache calls back to us.
429 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000430 xfs_iunlock(ip, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
Nathan Scott1b895842006-03-31 13:08:59 +1000431
Eric Sandeen24ee8082006-01-11 15:34:32 +1100432 zero_len = mp->m_sb.sb_blocksize - zero_offset;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100433 if (isize + zero_len > offset)
434 zero_len = offset - isize;
435 error = xfs_iozero(ip, isize, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000437 xfs_ilock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 ASSERT(error >= 0);
439 return error;
440}
441
442/*
443 * Zero any on disk space between the current EOF and the new,
444 * larger EOF. This handles the normal case of zeroing the remainder
445 * of the last block in the file and the unusual case of zeroing blocks
446 * out beyond the size of the file. This second case only happens
447 * with fixed size extents and when the system crashes before the inode
448 * size was updated but after blocks were allocated. If fill is set,
449 * then any holes in the range are filled and zeroed. If not, the holes
450 * are left alone as holes.
451 */
452
453int /* error (positive) */
454xfs_zero_eof(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000455 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 xfs_off_t offset, /* starting I/O offset */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100457 xfs_fsize_t isize) /* current inode size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Christoph Hellwig613d7042007-10-11 17:44:08 +1000459 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 xfs_fileoff_t start_zero_fsb;
461 xfs_fileoff_t end_zero_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 xfs_fileoff_t zero_count_fsb;
463 xfs_fileoff_t last_fsb;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100464 xfs_fileoff_t zero_off;
465 xfs_fsize_t zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int nimaps;
467 int error = 0;
468 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Christoph Hellwig613d7042007-10-11 17:44:08 +1000470 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
471 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
Eric Sandeen24ee8082006-01-11 15:34:32 +1100472 ASSERT(offset > isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /*
475 * First handle zeroing the block on which isize resides.
476 * We only zero a part of that block so it is handled specially.
477 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000478 error = xfs_zero_last_block(ip, offset, isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (error) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000480 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
481 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return error;
483 }
484
485 /*
486 * Calculate the range between the new size and the old
487 * where blocks needing to be zeroed may exist. To get the
488 * block where the last byte in the file currently resides,
489 * we need to subtract one from the size and truncate back
490 * to a block boundary. We subtract 1 in case the size is
491 * exactly on a block boundary.
492 */
493 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
494 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
495 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
496 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
497 if (last_fsb == end_zero_fsb) {
498 /*
499 * The size was only incremented on its last block.
500 * We took care of that above, so just return.
501 */
502 return 0;
503 }
504
505 ASSERT(start_zero_fsb <= end_zero_fsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 while (start_zero_fsb <= end_zero_fsb) {
507 nimaps = 1;
508 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000509 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000510 0, NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (error) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000512 ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
513 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return error;
515 }
516 ASSERT(nimaps > 0);
517
518 if (imap.br_state == XFS_EXT_UNWRITTEN ||
519 imap.br_startblock == HOLESTARTBLOCK) {
520 /*
521 * This loop handles initializing pages that were
522 * partially initialized by the code below this
523 * loop. It basically zeroes the part of the page
524 * that sits on a hole and sets the page as P_HOLE
525 * and calls remapf if it is a mapped file.
526 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100527 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
529 continue;
530 }
531
532 /*
Nathan Scott3d80ede2006-06-09 14:57:30 +1000533 * There are blocks we need to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * Drop the inode lock while we're doing the I/O.
535 * We'll still have the iolock to protect us.
536 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000537 xfs_iunlock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Lachlan McIlroy68160162007-02-10 18:36:47 +1100539 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
540 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
541
542 if ((zero_off + zero_len) > offset)
543 zero_len = offset - zero_off;
544
545 error = xfs_iozero(ip, zero_off, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (error) {
547 goto out_lock;
548 }
549
Nathan Scott3d80ede2006-06-09 14:57:30 +1000550 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
552
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000553 xfs_ilock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
556 return 0;
557
558out_lock:
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 ASSERT(error >= 0);
561 return error;
562}
563
564ssize_t /* bytes written, or (-) error */
565xfs_write(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000566 struct xfs_inode *xip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct kiocb *iocb,
568 const struct iovec *iovp,
569 unsigned int nsegs,
570 loff_t *offset,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000571 int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct file *file = iocb->ki_filp;
574 struct address_space *mapping = file->f_mapping;
575 struct inode *inode = mapping->host;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000576 bhv_vnode_t *vp = XFS_ITOV(xip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 unsigned long segs = nsegs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 xfs_mount_t *mp;
579 ssize_t ret = 0, error = 0;
580 xfs_fsize_t isize, new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 int iolock;
582 int eventsent = 0;
Nathan Scott8285fb52006-06-09 17:07:12 +1000583 bhv_vrwlock_t locktype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 size_t ocount = 0, count;
585 loff_t pos;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000586 int need_i_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 XFS_STATS_INC(xs_write_calls);
589
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -0700590 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
591 if (error)
592 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 count = ocount;
595 pos = *offset;
596
597 if (count == 0)
598 return 0;
599
Christoph Hellwig613d7042007-10-11 17:44:08 +1000600 mp = xip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000602 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
Nathan Scott34327e12006-06-09 17:11:55 +1000603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (XFS_FORCED_SHUTDOWN(mp))
605 return -EIO;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607relock:
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000608 if (ioflags & IO_ISDIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 iolock = XFS_IOLOCK_SHARED;
610 locktype = VRWLOCK_WRITE_DIRECT;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000611 need_i_mutex = 0;
612 } else {
613 iolock = XFS_IOLOCK_EXCL;
614 locktype = VRWLOCK_WRITE;
615 need_i_mutex = 1;
616 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621start:
622 error = -generic_write_checks(file, &pos, &count,
623 S_ISBLK(inode->i_mode));
624 if (error) {
625 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100626 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000629 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 !(ioflags & IO_INVIS) && !eventsent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int dmflags = FILP_DELAY_FLAG(file);
632
Nathan Scotta13828b2006-03-17 17:26:14 +1100633 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800634 dmflags |= DM_FLAGS_IMUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 xfs_iunlock(xip, XFS_ILOCK_EXCL);
637 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
638 pos, count,
639 dmflags, &locktype);
640 if (error) {
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000641 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 xfs_ilock(xip, XFS_ILOCK_EXCL);
644 eventsent = 1;
645
646 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000647 * The iolock was dropped and reacquired in XFS_SEND_DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * so we have to recheck the size when appending.
649 * We will only "goto start;" once, since having sent the
650 * event prevents another call to XFS_SEND_DATA, which is
651 * what allows the size to change in the first place.
652 */
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000653 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
654 goto start;
655 }
656
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000657 if (ioflags & IO_ISDIRECT) {
658 xfs_buftarg_t *target =
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100659 XFS_IS_REALTIME_INODE(xip) ?
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000660 mp->m_rtdev_targp : mp->m_ddev_targp;
661
662 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
663 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
664 return XFS_ERROR(-EINVAL);
665 }
666
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000667 if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000668 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
669 iolock = XFS_IOLOCK_EXCL;
670 locktype = VRWLOCK_WRITE;
671 need_i_mutex = 1;
672 mutex_lock(&inode->i_mutex);
673 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto start;
675 }
676 }
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 new_size = pos + count;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000679 if (new_size > xip->i_size)
Christoph Hellwig613d7042007-10-11 17:44:08 +1000680 xip->i_new_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Nathan Scott4aeb6642005-11-02 11:43:58 +1100682 if (likely(!(ioflags & IO_INVIS))) {
Christoph Hellwig870f4812006-01-09 20:52:01 -0800683 file_update_time(file);
Nathan Scott4aeb6642005-11-02 11:43:58 +1100684 xfs_ichgtime_fast(xip, inode,
685 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 /*
689 * If the offset is beyond the size of the file, we have a couple
690 * of things to do. First, if there is already space allocated
691 * we need to either create holes or zero the disk or ...
692 *
693 * If there is a page where the previous size lands, we need
694 * to zero it out up to the new size.
695 */
696
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000697 if (pos > xip->i_size) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000698 error = xfs_zero_eof(xip, pos, xip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (error) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000700 xfs_iunlock(xip, XFS_ILOCK_EXCL);
701 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 }
704 xfs_iunlock(xip, XFS_ILOCK_EXCL);
705
706 /*
707 * If we're writing the file then make sure to clear the
708 * setuid and setgid bits if the process is not being run
709 * by root. This keeps people from modifying setuid and
710 * setgid binaries.
711 */
712
713 if (((xip->i_d.di_mode & S_ISUID) ||
714 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
715 (S_ISGID | S_IXGRP))) &&
716 !capable(CAP_FSETID)) {
717 error = xfs_write_clear_setuid(xip);
718 if (likely(!error))
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800719 error = -remove_suid(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (unlikely(error)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000721 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723 }
724
725retry:
726 /* We can write back this queue in page reclaim */
727 current->backing_dev_info = mapping->backing_dev_info;
728
729 if ((ioflags & IO_ISDIRECT)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000730 if (VN_CACHED(vp)) {
731 WARN_ON(need_i_mutex == 0);
Christoph Hellwig613d7042007-10-11 17:44:08 +1000732 xfs_inval_cached_trace(xip, pos, -1,
Tim Shimmine6a4b372007-11-23 16:30:42 +1100733 (pos & PAGE_CACHE_MASK), -1);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000734 error = xfs_flushinval_pages(xip,
Tim Shimmine6a4b372007-11-23 16:30:42 +1100735 (pos & PAGE_CACHE_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000737 if (error)
738 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
Nathan Scotta13828b2006-03-17 17:26:14 +1100741 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* demote the lock now the cached pages are gone */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000743 xfs_ilock_demote(xip, XFS_IOLOCK_EXCL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800744 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 iolock = XFS_IOLOCK_SHARED;
747 locktype = VRWLOCK_WRITE_DIRECT;
Nathan Scotta13828b2006-03-17 17:26:14 +1100748 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
Christoph Hellwig613d7042007-10-11 17:44:08 +1000751 xfs_rw_enter_trace(XFS_DIOWR_ENTER, xip, (void *)iovp, segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 *offset, ioflags);
753 ret = generic_file_direct_write(iocb, iovp,
754 &segs, pos, offset, count, ocount);
755
756 /*
757 * direct-io write to a hole: fall through to buffered I/O
758 * for completing the rest of the request.
759 */
760 if (ret >= 0 && ret != count) {
761 XFS_STATS_ADD(xs_write_bytes, ret);
762
763 pos += ret;
764 count -= ret;
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ioflags &= ~IO_ISDIRECT;
767 xfs_iunlock(xip, iolock);
768 goto relock;
769 }
770 } else {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000771 xfs_rw_enter_trace(XFS_WRITE_ENTER, xip, (void *)iovp, segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 *offset, ioflags);
773 ret = generic_file_buffered_write(iocb, iovp, segs,
774 pos, offset, count, ret);
775 }
776
777 current->backing_dev_info = NULL;
778
779 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
780 ret = wait_on_sync_kiocb(iocb);
781
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000782 if (ret == -ENOSPC &&
783 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
Christoph Hellwig993386c2007-08-28 16:12:30 +1000784 xfs_rwunlock(xip, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100785 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800786 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
788 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
789 0, 0, 0); /* Delay flag intentionally unused */
Nathan Scotta13828b2006-03-17 17:26:14 +1100790 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800791 mutex_lock(&inode->i_mutex);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000792 xfs_rwlock(xip, locktype);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000793 if (error)
794 goto out_unlock_internal;
795 pos = xip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 ret = 0;
797 goto retry;
798 }
799
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100800 isize = i_size_read(inode);
801 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
802 *offset = isize;
803
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000804 if (*offset > xip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 xfs_ilock(xip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000806 if (*offset > xip->i_size)
807 xip->i_size = *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 xfs_iunlock(xip, XFS_ILOCK_EXCL);
809 }
810
811 error = -ret;
812 if (ret <= 0)
813 goto out_unlock_internal;
814
815 XFS_STATS_ADD(xs_write_bytes, ret);
816
817 /* Handle various SYNC-type writes */
818 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000819 int error2;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000820 xfs_rwunlock(xip, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100821 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800822 mutex_unlock(&inode->i_mutex);
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000823 error2 = sync_page_range(inode, mapping, pos, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!error)
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000825 error = error2;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000826 if (need_i_mutex)
827 mutex_lock(&inode->i_mutex);
Christoph Hellwig993386c2007-08-28 16:12:30 +1000828 xfs_rwlock(xip, locktype);
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000829 error2 = xfs_write_sync_logforce(mp, xip);
830 if (!error)
831 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
834 out_unlock_internal:
Christoph Hellwig613d7042007-10-11 17:44:08 +1000835 if (xip->i_new_size) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000836 xfs_ilock(xip, XFS_ILOCK_EXCL);
Christoph Hellwig613d7042007-10-11 17:44:08 +1000837 xip->i_new_size = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000838 /*
839 * If this was a direct or synchronous I/O that failed (such
840 * as ENOSPC) then part of the I/O may have been written to
841 * disk before the error occured. In this case the on-disk
842 * file size may have been adjusted beyond the in-memory file
843 * size and now needs to be truncated back.
844 */
845 if (xip->i_d.di_size > xip->i_size)
846 xip->i_d.di_size = xip->i_size;
847 xfs_iunlock(xip, XFS_ILOCK_EXCL);
848 }
Christoph Hellwig993386c2007-08-28 16:12:30 +1000849 xfs_rwunlock(xip, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100850 out_unlock_mutex:
851 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800852 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return -error;
854}
855
856/*
857 * All xfs metadata buffers except log state machine buffers
858 * get this attached as their b_bdstrat callback function.
859 * This is so that we can catch a buffer
860 * after prematurely unpinning it to forcibly shutdown the filesystem.
861 */
862int
863xfs_bdstrat_cb(struct xfs_buf *bp)
864{
865 xfs_mount_t *mp;
866
867 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
868 if (!XFS_FORCED_SHUTDOWN(mp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100869 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return 0;
871 } else {
872 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
873 /*
874 * Metadata write that didn't get logged but
875 * written delayed anyway. These aren't associated
876 * with a transaction, and can be ignored.
877 */
878 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
879 (XFS_BUF_ISREAD(bp)) == 0)
880 return (xfs_bioerror_relse(bp));
881 else
882 return (xfs_bioerror(bp));
883 }
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/*
887 * Wrapper around bdstrat so that we can stop data
888 * from going to disk in case we are shutting down the filesystem.
889 * Typically user data goes thru this path; one of the exceptions
890 * is the superblock.
891 */
892int
893xfsbdstrat(
894 struct xfs_mount *mp,
895 struct xfs_buf *bp)
896{
897 ASSERT(mp);
898 if (!XFS_FORCED_SHUTDOWN(mp)) {
899 /* Grio redirection would go here
900 * if (XFS_BUF_IS_GRIO(bp)) {
901 */
902
Nathan Scottce8e9222006-01-11 15:39:08 +1100903 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return 0;
905 }
906
907 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
908 return (xfs_bioerror_relse(bp));
909}
910
911/*
912 * If the underlying (data/log/rt) device is readonly, there are some
913 * operations that cannot proceed.
914 */
915int
916xfs_dev_is_read_only(
917 xfs_mount_t *mp,
918 char *message)
919{
920 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
921 xfs_readonly_buftarg(mp->m_logdev_targp) ||
922 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
923 cmn_err(CE_NOTE,
924 "XFS: %s required on read-only device.", message);
925 cmn_err(CE_NOTE,
926 "XFS: write access unavailable, cannot proceed.");
927 return EROFS;
928 }
929 return 0;
930}