blob: 072050f8d34634d59e4d200dfe14218bcf0e75ad [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_attr.h"
46#include "xfs_inode_item.h"
47#include "xfs_buf_item.h"
48#include "xfs_utils.h"
49#include "xfs_iomap.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100050#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <linux/capability.h>
53#include <linux/writeback.h>
54
55
56#if defined(XFS_RW_TRACE)
57void
58xfs_rw_enter_trace(
59 int tag,
Christoph Hellwig613d7042007-10-11 17:44:08 +100060 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 void *data,
62 size_t segs,
63 loff_t offset,
64 int ioflags)
65{
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (ip->i_rwtrace == NULL)
67 return;
68 ktrace_enter(ip->i_rwtrace,
69 (void *)(unsigned long)tag,
70 (void *)ip,
71 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
72 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
73 (void *)data,
74 (void *)((unsigned long)segs),
75 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
76 (void *)((unsigned long)(offset & 0xffffffff)),
77 (void *)((unsigned long)ioflags),
Christoph Hellwig613d7042007-10-11 17:44:08 +100078 (void *)((unsigned long)((ip->i_new_size >> 32) & 0xffffffff)),
79 (void *)((unsigned long)(ip->i_new_size & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +110080 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 (void *)NULL,
82 (void *)NULL,
83 (void *)NULL,
84 (void *)NULL);
85}
86
87void
88xfs_inval_cached_trace(
Christoph Hellwig613d7042007-10-11 17:44:08 +100089 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 xfs_off_t offset,
91 xfs_off_t len,
92 xfs_off_t first,
93 xfs_off_t last)
94{
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if (ip->i_rwtrace == NULL)
97 return;
98 ktrace_enter(ip->i_rwtrace,
99 (void *)(__psint_t)XFS_INVAL_CACHED,
100 (void *)ip,
101 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
102 (void *)((unsigned long)(offset & 0xffffffff)),
103 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
104 (void *)((unsigned long)(len & 0xffffffff)),
105 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
106 (void *)((unsigned long)(first & 0xffffffff)),
107 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
108 (void *)((unsigned long)(last & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +1100109 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 (void *)NULL,
111 (void *)NULL,
112 (void *)NULL,
113 (void *)NULL,
114 (void *)NULL);
115}
116#endif
117
118/*
119 * xfs_iozero
120 *
121 * xfs_iozero clears the specified range of buffer supplied,
122 * and marks all the affected blocks as valid and modified. If
123 * an affected block is not allocated, it will be allocated. If
124 * an affected block is not completely overwritten, and is not
125 * valid before the operation, it will be read from disk before
126 * being partially zeroed.
127 */
128STATIC int
129xfs_iozero(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000130 struct xfs_inode *ip, /* inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 loff_t pos, /* offset in file */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100132 size_t count) /* size of data to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct page *page;
135 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int status;
137
David Chinner01651642008-08-13 15:45:15 +1000138 mapping = VFS_I(ip)->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 do {
Nick Piggind79689c2007-10-16 01:25:06 -0700140 unsigned offset, bytes;
141 void *fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 bytes = PAGE_CACHE_SIZE - offset;
145 if (bytes > count)
146 bytes = count;
147
Nick Piggind79689c2007-10-16 01:25:06 -0700148 status = pagecache_write_begin(NULL, mapping, pos, bytes,
149 AOP_FLAG_UNINTERRUPTIBLE,
150 &page, &fsdata);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100151 if (status)
Nick Piggind79689c2007-10-16 01:25:06 -0700152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800154 zero_user(page, offset, bytes);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100155
Nick Piggind79689c2007-10-16 01:25:06 -0700156 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
157 page, fsdata);
158 WARN_ON(status <= 0); /* can't return less than zero! */
159 pos += bytes;
160 count -= bytes;
161 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 } while (count);
163
164 return (-status);
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167ssize_t /* bytes read, or (-) error */
168xfs_read(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000169 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 struct kiocb *iocb,
171 const struct iovec *iovp,
172 unsigned int segs,
173 loff_t *offset,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000174 int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct file *file = iocb->ki_filp;
177 struct inode *inode = file->f_mapping->host;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000178 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 size_t size = 0;
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000180 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 xfs_fsize_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned long seg;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 XFS_STATS_INC(xs_read_calls);
186
187 /* START copy & waste from filemap.c */
188 for (seg = 0; seg < segs; seg++) {
189 const struct iovec *iv = &iovp[seg];
190
191 /*
192 * If any segment has a negative length, or the cumulative
193 * length ever wraps negative then return -EINVAL.
194 */
195 size += iv->iov_len;
196 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
197 return XFS_ERROR(-EINVAL);
198 }
199 /* END copy & waste from filemap.c */
200
201 if (unlikely(ioflags & IO_ISDIRECT)) {
202 xfs_buftarg_t *target =
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100203 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 mp->m_rtdev_targp : mp->m_ddev_targp;
Nathan Scottce8e9222006-01-11 15:39:08 +1100205 if ((*offset & target->bt_smask) ||
206 (size & target->bt_smask)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000207 if (*offset == ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return (0);
209 }
210 return -XFS_ERROR(EINVAL);
211 }
212 }
213
214 n = XFS_MAXIOFFSET(mp) - *offset;
215 if ((n <= 0) || (size == 0))
216 return 0;
217
218 if (n < size)
219 size = n;
220
Nathan Scotta13828b2006-03-17 17:26:14 +1100221 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (unlikely(ioflags & IO_ISDIRECT))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800225 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 xfs_ilock(ip, XFS_IOLOCK_SHARED);
227
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000228 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000229 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
Christoph Hellwig126468b2008-03-06 13:44:57 +1100230 int iolock = XFS_IOLOCK_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100232 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *offset, size,
Christoph Hellwig126468b2008-03-06 13:44:57 +1100233 dmflags, &iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (ret) {
235 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000236 if (unlikely(ioflags & IO_ISDIRECT))
237 mutex_unlock(&inode->i_mutex);
238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 }
241
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000242 if (unlikely(ioflags & IO_ISDIRECT)) {
Christoph Hellwigdcf49cc2008-03-06 13:46:37 +1100243 if (inode->i_mapping->nrpages)
Dave Chinner2e656092008-11-28 14:23:33 +1100244 ret = -xfs_flushinval_pages(ip, (*offset & PAGE_CACHE_MASK),
Tim Shimmine6a4b372007-11-23 16:30:42 +1100245 -1, FI_REMAPF_LOCKED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000246 mutex_unlock(&inode->i_mutex);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000247 if (ret) {
248 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
249 return ret;
250 }
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000251 }
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000252
Christoph Hellwig613d7042007-10-11 17:44:08 +1000253 xfs_rw_enter_trace(XFS_READ_ENTER, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 (void *)iovp, segs, *offset, ioflags);
Badari Pulavarty543ade12006-09-30 23:28:48 -0700255
256 iocb->ki_pos = *offset;
257 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
259 ret = wait_on_sync_kiocb(iocb);
260 if (ret > 0)
261 XFS_STATS_ADD(xs_read_bytes, ret);
262
263 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return ret;
265}
266
267ssize_t
Nathan Scott1b895842006-03-31 13:08:59 +1000268xfs_splice_read(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000269 xfs_inode_t *ip,
Nathan Scott1b895842006-03-31 13:08:59 +1000270 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200271 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200272 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000273 size_t count,
274 int flags,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000275 int ioflags)
Nathan Scott1b895842006-03-31 13:08:59 +1000276{
Nathan Scott1b895842006-03-31 13:08:59 +1000277 xfs_mount_t *mp = ip->i_mount;
278 ssize_t ret;
279
280 XFS_STATS_INC(xs_read_calls);
281 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
282 return -EIO;
283
284 xfs_ilock(ip, XFS_IOLOCK_SHARED);
285
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000286 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Christoph Hellwig126468b2008-03-06 13:44:57 +1100287 int iolock = XFS_IOLOCK_SHARED;
Nathan Scott1b895842006-03-31 13:08:59 +1000288 int error;
289
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100290 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *ppos, count,
Christoph Hellwig126468b2008-03-06 13:44:57 +1100291 FILP_DELAY_FLAG(infilp), &iolock);
Nathan Scott1b895842006-03-31 13:08:59 +1000292 if (error) {
293 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
294 return -error;
295 }
296 }
Christoph Hellwig613d7042007-10-11 17:44:08 +1000297 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, ip,
Jens Axboecbb7e572006-04-11 14:57:50 +0200298 pipe, count, *ppos, ioflags);
299 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000300 if (ret > 0)
301 XFS_STATS_ADD(xs_read_bytes, ret);
302
303 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
304 return ret;
305}
306
307ssize_t
308xfs_splice_write(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000309 xfs_inode_t *ip,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200310 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000311 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200312 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000313 size_t count,
314 int flags,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000315 int ioflags)
Nathan Scott1b895842006-03-31 13:08:59 +1000316{
Nathan Scott1b895842006-03-31 13:08:59 +1000317 xfs_mount_t *mp = ip->i_mount;
318 ssize_t ret;
David Chinner0a8d17d2006-09-07 14:27:15 +1000319 struct inode *inode = outfilp->f_mapping->host;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000320 xfs_fsize_t isize, new_size;
Nathan Scott1b895842006-03-31 13:08:59 +1000321
322 XFS_STATS_INC(xs_write_calls);
323 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
324 return -EIO;
325
326 xfs_ilock(ip, XFS_IOLOCK_EXCL);
327
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000328 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
Christoph Hellwig126468b2008-03-06 13:44:57 +1100329 int iolock = XFS_IOLOCK_EXCL;
Nathan Scott1b895842006-03-31 13:08:59 +1000330 int error;
331
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100332 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, *ppos, count,
Christoph Hellwig126468b2008-03-06 13:44:57 +1100333 FILP_DELAY_FLAG(outfilp), &iolock);
Nathan Scott1b895842006-03-31 13:08:59 +1000334 if (error) {
335 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
336 return -error;
337 }
338 }
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000339
340 new_size = *ppos + count;
341
342 xfs_ilock(ip, XFS_ILOCK_EXCL);
343 if (new_size > ip->i_size)
Christoph Hellwig613d7042007-10-11 17:44:08 +1000344 ip->i_new_size = new_size;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000345 xfs_iunlock(ip, XFS_ILOCK_EXCL);
346
Christoph Hellwig613d7042007-10-11 17:44:08 +1000347 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, ip,
Jens Axboecbb7e572006-04-11 14:57:50 +0200348 pipe, count, *ppos, ioflags);
349 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000350 if (ret > 0)
351 XFS_STATS_ADD(xs_write_bytes, ret);
352
David Chinner0a8d17d2006-09-07 14:27:15 +1000353 isize = i_size_read(inode);
354 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
355 *ppos = isize;
356
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000357 if (*ppos > ip->i_size) {
David Chinner0a8d17d2006-09-07 14:27:15 +1000358 xfs_ilock(ip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000359 if (*ppos > ip->i_size)
360 ip->i_size = *ppos;
361 xfs_iunlock(ip, XFS_ILOCK_EXCL);
362 }
363
Christoph Hellwig613d7042007-10-11 17:44:08 +1000364 if (ip->i_new_size) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000365 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig613d7042007-10-11 17:44:08 +1000366 ip->i_new_size = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000367 if (ip->i_d.di_size > ip->i_size)
368 ip->i_d.di_size = ip->i_size;
David Chinner0a8d17d2006-09-07 14:27:15 +1000369 xfs_iunlock(ip, XFS_ILOCK_EXCL);
370 }
Nathan Scott1b895842006-03-31 13:08:59 +1000371 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return ret;
373}
374
375/*
376 * This routine is called to handle zeroing any space in the last
377 * block of the file that is beyond the EOF. We do this since the
378 * size is being increased without writing anything to that block
379 * and we don't want anyone to read the garbage on the disk.
380 */
381STATIC int /* error (positive) */
382xfs_zero_last_block(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000383 xfs_inode_t *ip,
Lachlan McIlroy68160162007-02-10 18:36:47 +1100384 xfs_fsize_t offset,
385 xfs_fsize_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 xfs_fileoff_t last_fsb;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000388 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 int nimaps;
390 int zero_offset;
391 int zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 int error = 0;
393 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000395 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Eric Sandeen24ee8082006-01-11 15:34:32 +1100397 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
398 if (zero_offset == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
400 * There are no extra bytes in the last block on disk to
401 * zero, so return.
402 */
403 return 0;
404 }
405
406 last_fsb = XFS_B_TO_FSBT(mp, isize);
407 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000408 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000409 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (error) {
411 return error;
412 }
413 ASSERT(nimaps > 0);
414 /*
415 * If the block underlying isize is just a hole, then there
416 * is nothing to zero.
417 */
418 if (imap.br_startblock == HOLESTARTBLOCK) {
419 return 0;
420 }
421 /*
422 * Zero the part of the last block beyond the EOF, and write it
423 * out sync. We need to drop the ilock while we do this so we
424 * don't deadlock when the buffer cache calls back to us.
425 */
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000426 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Nathan Scott1b895842006-03-31 13:08:59 +1000427
Eric Sandeen24ee8082006-01-11 15:34:32 +1100428 zero_len = mp->m_sb.sb_blocksize - zero_offset;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100429 if (isize + zero_len > offset)
430 zero_len = offset - isize;
431 error = xfs_iozero(ip, isize, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000433 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 ASSERT(error >= 0);
435 return error;
436}
437
438/*
439 * Zero any on disk space between the current EOF and the new,
440 * larger EOF. This handles the normal case of zeroing the remainder
441 * of the last block in the file and the unusual case of zeroing blocks
442 * out beyond the size of the file. This second case only happens
443 * with fixed size extents and when the system crashes before the inode
444 * size was updated but after blocks were allocated. If fill is set,
445 * then any holes in the range are filled and zeroed. If not, the holes
446 * are left alone as holes.
447 */
448
449int /* error (positive) */
450xfs_zero_eof(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000451 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 xfs_off_t offset, /* starting I/O offset */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100453 xfs_fsize_t isize) /* current inode size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Christoph Hellwig613d7042007-10-11 17:44:08 +1000455 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 xfs_fileoff_t start_zero_fsb;
457 xfs_fileoff_t end_zero_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 xfs_fileoff_t zero_count_fsb;
459 xfs_fileoff_t last_fsb;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100460 xfs_fileoff_t zero_off;
461 xfs_fsize_t zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int nimaps;
463 int error = 0;
464 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000466 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Eric Sandeen24ee8082006-01-11 15:34:32 +1100467 ASSERT(offset > isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /*
470 * First handle zeroing the block on which isize resides.
471 * We only zero a part of that block so it is handled specially.
472 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000473 error = xfs_zero_last_block(ip, offset, isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (error) {
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000475 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return error;
477 }
478
479 /*
480 * Calculate the range between the new size and the old
481 * where blocks needing to be zeroed may exist. To get the
482 * block where the last byte in the file currently resides,
483 * we need to subtract one from the size and truncate back
484 * to a block boundary. We subtract 1 in case the size is
485 * exactly on a block boundary.
486 */
487 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
488 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
489 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
490 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
491 if (last_fsb == end_zero_fsb) {
492 /*
493 * The size was only incremented on its last block.
494 * We took care of that above, so just return.
495 */
496 return 0;
497 }
498
499 ASSERT(start_zero_fsb <= end_zero_fsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 while (start_zero_fsb <= end_zero_fsb) {
501 nimaps = 1;
502 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000503 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000504 0, NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (error) {
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000506 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return error;
508 }
509 ASSERT(nimaps > 0);
510
511 if (imap.br_state == XFS_EXT_UNWRITTEN ||
512 imap.br_startblock == HOLESTARTBLOCK) {
513 /*
514 * This loop handles initializing pages that were
515 * partially initialized by the code below this
516 * loop. It basically zeroes the part of the page
517 * that sits on a hole and sets the page as P_HOLE
518 * and calls remapf if it is a mapped file.
519 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100520 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
522 continue;
523 }
524
525 /*
Nathan Scott3d80ede2006-06-09 14:57:30 +1000526 * There are blocks we need to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * Drop the inode lock while we're doing the I/O.
528 * We'll still have the iolock to protect us.
529 */
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000530 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Lachlan McIlroy68160162007-02-10 18:36:47 +1100532 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
533 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
534
535 if ((zero_off + zero_len) > offset)
536 zero_len = offset - zero_off;
537
538 error = xfs_iozero(ip, zero_off, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (error) {
540 goto out_lock;
541 }
542
Nathan Scott3d80ede2006-06-09 14:57:30 +1000543 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
545
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000546 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 return 0;
550
551out_lock:
Christoph Hellwig579aa9ca2008-04-22 17:34:00 +1000552 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ASSERT(error >= 0);
554 return error;
555}
556
557ssize_t /* bytes written, or (-) error */
558xfs_write(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000559 struct xfs_inode *xip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct kiocb *iocb,
561 const struct iovec *iovp,
562 unsigned int nsegs,
563 loff_t *offset,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000564 int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct file *file = iocb->ki_filp;
567 struct address_space *mapping = file->f_mapping;
568 struct inode *inode = mapping->host;
569 unsigned long segs = nsegs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 xfs_mount_t *mp;
571 ssize_t ret = 0, error = 0;
572 xfs_fsize_t isize, new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int iolock;
574 int eventsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 size_t ocount = 0, count;
576 loff_t pos;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000577 int need_i_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 XFS_STATS_INC(xs_write_calls);
580
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -0700581 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
582 if (error)
583 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 count = ocount;
586 pos = *offset;
587
588 if (count == 0)
589 return 0;
590
Christoph Hellwig613d7042007-10-11 17:44:08 +1000591 mp = xip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000593 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
Nathan Scott34327e12006-06-09 17:11:55 +1000594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (XFS_FORCED_SHUTDOWN(mp))
596 return -EIO;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598relock:
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000599 if (ioflags & IO_ISDIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 iolock = XFS_IOLOCK_SHARED;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000601 need_i_mutex = 0;
602 } else {
603 iolock = XFS_IOLOCK_EXCL;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000604 need_i_mutex = 1;
605 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610start:
611 error = -generic_write_checks(file, &pos, &count,
612 S_ISBLK(inode->i_mode));
613 if (error) {
614 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100615 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000618 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 !(ioflags & IO_INVIS) && !eventsent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int dmflags = FILP_DELAY_FLAG(file);
621
Nathan Scotta13828b2006-03-17 17:26:14 +1100622 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800623 dmflags |= DM_FLAGS_IMUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 xfs_iunlock(xip, XFS_ILOCK_EXCL);
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100626 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, xip,
Christoph Hellwig126468b2008-03-06 13:44:57 +1100627 pos, count, dmflags, &iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (error) {
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000629 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631 xfs_ilock(xip, XFS_ILOCK_EXCL);
632 eventsent = 1;
633
634 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000635 * The iolock was dropped and reacquired in XFS_SEND_DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 * so we have to recheck the size when appending.
637 * We will only "goto start;" once, since having sent the
638 * event prevents another call to XFS_SEND_DATA, which is
639 * what allows the size to change in the first place.
640 */
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000641 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
642 goto start;
643 }
644
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000645 if (ioflags & IO_ISDIRECT) {
646 xfs_buftarg_t *target =
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100647 XFS_IS_REALTIME_INODE(xip) ?
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000648 mp->m_rtdev_targp : mp->m_ddev_targp;
649
650 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
651 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
652 return XFS_ERROR(-EINVAL);
653 }
654
Christoph Hellwigdcf49cc2008-03-06 13:46:37 +1100655 if (!need_i_mutex && (mapping->nrpages || pos > xip->i_size)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000656 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
657 iolock = XFS_IOLOCK_EXCL;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000658 need_i_mutex = 1;
659 mutex_lock(&inode->i_mutex);
660 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 goto start;
662 }
663 }
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 new_size = pos + count;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000666 if (new_size > xip->i_size)
Christoph Hellwig613d7042007-10-11 17:44:08 +1000667 xip->i_new_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Christoph Hellwig2234d542008-12-03 12:20:22 +0100669 if (likely(!(ioflags & IO_INVIS)))
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000670 file_update_time(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /*
673 * If the offset is beyond the size of the file, we have a couple
674 * of things to do. First, if there is already space allocated
675 * we need to either create holes or zero the disk or ...
676 *
677 * If there is a page where the previous size lands, we need
678 * to zero it out up to the new size.
679 */
680
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000681 if (pos > xip->i_size) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000682 error = xfs_zero_eof(xip, pos, xip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (error) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000684 xfs_iunlock(xip, XFS_ILOCK_EXCL);
685 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 }
688 xfs_iunlock(xip, XFS_ILOCK_EXCL);
689
690 /*
691 * If we're writing the file then make sure to clear the
692 * setuid and setgid bits if the process is not being run
693 * by root. This keeps people from modifying setuid and
694 * setgid binaries.
695 */
696
697 if (((xip->i_d.di_mode & S_ISUID) ||
698 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
699 (S_ISGID | S_IXGRP))) &&
700 !capable(CAP_FSETID)) {
701 error = xfs_write_clear_setuid(xip);
702 if (likely(!error))
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200703 error = -file_remove_suid(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (unlikely(error)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000705 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 }
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* We can write back this queue in page reclaim */
710 current->backing_dev_info = mapping->backing_dev_info;
711
712 if ((ioflags & IO_ISDIRECT)) {
Christoph Hellwigdcf49cc2008-03-06 13:46:37 +1100713 if (mapping->nrpages) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000714 WARN_ON(need_i_mutex == 0);
Christoph Hellwig613d7042007-10-11 17:44:08 +1000715 xfs_inval_cached_trace(xip, pos, -1,
Tim Shimmine6a4b372007-11-23 16:30:42 +1100716 (pos & PAGE_CACHE_MASK), -1);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000717 error = xfs_flushinval_pages(xip,
Tim Shimmine6a4b372007-11-23 16:30:42 +1100718 (pos & PAGE_CACHE_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000720 if (error)
721 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
Nathan Scotta13828b2006-03-17 17:26:14 +1100724 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* demote the lock now the cached pages are gone */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000726 xfs_ilock_demote(xip, XFS_IOLOCK_EXCL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800727 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 iolock = XFS_IOLOCK_SHARED;
Nathan Scotta13828b2006-03-17 17:26:14 +1100730 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
Christoph Hellwig613d7042007-10-11 17:44:08 +1000733 xfs_rw_enter_trace(XFS_DIOWR_ENTER, xip, (void *)iovp, segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 *offset, ioflags);
735 ret = generic_file_direct_write(iocb, iovp,
736 &segs, pos, offset, count, ocount);
737
738 /*
739 * direct-io write to a hole: fall through to buffered I/O
740 * for completing the rest of the request.
741 */
742 if (ret >= 0 && ret != count) {
743 XFS_STATS_ADD(xs_write_bytes, ret);
744
745 pos += ret;
746 count -= ret;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 ioflags &= ~IO_ISDIRECT;
749 xfs_iunlock(xip, iolock);
750 goto relock;
751 }
752 } else {
Dave Chinner58252942009-04-06 18:45:44 +0200753 int enospc = 0;
754 ssize_t ret2 = 0;
755
756write_retry:
Christoph Hellwig613d7042007-10-11 17:44:08 +1000757 xfs_rw_enter_trace(XFS_WRITE_ENTER, xip, (void *)iovp, segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *offset, ioflags);
Dave Chinner58252942009-04-06 18:45:44 +0200759 ret2 = generic_file_buffered_write(iocb, iovp, segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 pos, offset, count, ret);
Dave Chinner58252942009-04-06 18:45:44 +0200761 /*
762 * if we just got an ENOSPC, flush the inode now we
763 * aren't holding any page locks and retry *once*
764 */
765 if (ret2 == -ENOSPC && !enospc) {
766 error = xfs_flush_pages(xip, 0, -1, 0, FI_NONE);
767 if (error)
768 goto out_unlock_internal;
769 enospc = 1;
770 goto write_retry;
771 }
772 ret = ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
775 current->backing_dev_info = NULL;
776
777 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
778 ret = wait_on_sync_kiocb(iocb);
779
Lachlan McIlroy25051152008-12-24 14:07:32 +1100780 isize = i_size_read(inode);
781 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
782 *offset = isize;
783
784 if (*offset > xip->i_size) {
785 xfs_ilock(xip, XFS_ILOCK_EXCL);
786 if (*offset > xip->i_size)
787 xip->i_size = *offset;
788 xfs_iunlock(xip, XFS_ILOCK_EXCL);
789 }
790
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000791 if (ret == -ENOSPC &&
792 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
Christoph Hellwig126468b2008-03-06 13:44:57 +1100793 xfs_iunlock(xip, iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100794 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800795 mutex_unlock(&inode->i_mutex);
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100796 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, xip,
797 DM_RIGHT_NULL, xip, DM_RIGHT_NULL, NULL, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 0, 0, 0); /* Delay flag intentionally unused */
Nathan Scotta13828b2006-03-17 17:26:14 +1100799 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800800 mutex_lock(&inode->i_mutex);
Christoph Hellwig126468b2008-03-06 13:44:57 +1100801 xfs_ilock(xip, iolock);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000802 if (error)
803 goto out_unlock_internal;
Lachlan McIlroy25051152008-12-24 14:07:32 +1100804 goto start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
807 error = -ret;
808 if (ret <= 0)
809 goto out_unlock_internal;
810
811 XFS_STATS_ADD(xs_write_bytes, ret);
812
813 /* Handle various SYNC-type writes */
814 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300815 loff_t end = pos + ret - 1;
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000816 int error2;
Christoph Hellwig126468b2008-03-06 13:44:57 +1100817
818 xfs_iunlock(xip, iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100819 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800820 mutex_unlock(&inode->i_mutex);
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300821
822 error2 = filemap_write_and_wait_range(mapping, pos, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!error)
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000824 error = error2;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000825 if (need_i_mutex)
826 mutex_lock(&inode->i_mutex);
Christoph Hellwig126468b2008-03-06 13:44:57 +1100827 xfs_ilock(xip, iolock);
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300828
829 error2 = xfs_fsync(xip);
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000830 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 Hellwig126468b2008-03-06 13:44:57 +1100849 xfs_iunlock(xip, iolock);
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{
Christoph Hellwig15ac08a2008-12-09 04:47:30 -0500865 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
867 /*
868 * Metadata write that didn't get logged but
869 * written delayed anyway. These aren't associated
870 * with a transaction, and can be ignored.
871 */
872 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
873 (XFS_BUF_ISREAD(bp)) == 0)
874 return (xfs_bioerror_relse(bp));
875 else
876 return (xfs_bioerror(bp));
877 }
Christoph Hellwig15ac08a2008-12-09 04:47:30 -0500878
879 xfs_buf_iorequest(bp);
880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/*
David Chinnerd64e31a2008-04-10 12:22:17 +1000884 * Wrapper around bdstrat so that we can stop data from going to disk in case
885 * we are shutting down the filesystem. Typically user data goes thru this
886 * path; one of the exceptions is the superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
David Chinnerd64e31a2008-04-10 12:22:17 +1000888void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889xfsbdstrat(
890 struct xfs_mount *mp,
891 struct xfs_buf *bp)
892{
893 ASSERT(mp);
David Chinnerd4055942008-04-17 16:49:35 +1000894 if (!XFS_FORCED_SHUTDOWN(mp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100895 xfs_buf_iorequest(bp);
David Chinnerd4055942008-04-17 16:49:35 +1000896 return;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
David Chinnerd64e31a2008-04-10 12:22:17 +1000900 xfs_bioerror_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903/*
904 * If the underlying (data/log/rt) device is readonly, there are some
905 * operations that cannot proceed.
906 */
907int
908xfs_dev_is_read_only(
909 xfs_mount_t *mp,
910 char *message)
911{
912 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
913 xfs_readonly_buftarg(mp->m_logdev_targp) ||
914 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
915 cmn_err(CE_NOTE,
916 "XFS: %s required on read-only device.", message);
917 cmn_err(CE_NOTE,
918 "XFS: write access unavailable, cannot proceed.");
919 return EROFS;
920 }
921 return 0;
922}