blob: eac6f80d786d5fa63834460c2f85ec905c8a99d4 [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"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000051#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include <linux/capability.h>
54#include <linux/writeback.h>
55
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * xfs_iozero
59 *
60 * xfs_iozero clears the specified range of buffer supplied,
61 * and marks all the affected blocks as valid and modified. If
62 * an affected block is not allocated, it will be allocated. If
63 * an affected block is not completely overwritten, and is not
64 * valid before the operation, it will be read from disk before
65 * being partially zeroed.
66 */
67STATIC int
68xfs_iozero(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +100069 struct xfs_inode *ip, /* inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 loff_t pos, /* offset in file */
Lachlan McIlroy68160162007-02-10 18:36:47 +110071 size_t count) /* size of data to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct page *page;
74 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int status;
76
David Chinner01651642008-08-13 15:45:15 +100077 mapping = VFS_I(ip)->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 do {
Nick Piggind79689c2007-10-16 01:25:06 -070079 unsigned offset, bytes;
80 void *fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 bytes = PAGE_CACHE_SIZE - offset;
84 if (bytes > count)
85 bytes = count;
86
Nick Piggind79689c2007-10-16 01:25:06 -070087 status = pagecache_write_begin(NULL, mapping, pos, bytes,
88 AOP_FLAG_UNINTERRUPTIBLE,
89 &page, &fsdata);
David Chinnere7ff6ae2007-02-10 18:37:46 +110090 if (status)
Nick Piggind79689c2007-10-16 01:25:06 -070091 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Christoph Lametereebd2aa2008-02-04 22:28:29 -080093 zero_user(page, offset, bytes);
David Chinnere7ff6ae2007-02-10 18:37:46 +110094
Nick Piggind79689c2007-10-16 01:25:06 -070095 status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
96 page, fsdata);
97 WARN_ON(status <= 0); /* can't return less than zero! */
98 pos += bytes;
99 count -= bytes;
100 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 } while (count);
102
103 return (-status);
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106ssize_t /* bytes read, or (-) error */
107xfs_read(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000108 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct kiocb *iocb,
110 const struct iovec *iovp,
111 unsigned int segs,
112 loff_t *offset,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000113 int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct file *file = iocb->ki_filp;
116 struct inode *inode = file->f_mapping->host;
Christoph Hellwig993386c2007-08-28 16:12:30 +1000117 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 size_t size = 0;
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000119 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 xfs_fsize_t n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned long seg;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 XFS_STATS_INC(xs_read_calls);
125
126 /* START copy & waste from filemap.c */
127 for (seg = 0; seg < segs; seg++) {
128 const struct iovec *iv = &iovp[seg];
129
130 /*
131 * If any segment has a negative length, or the cumulative
132 * length ever wraps negative then return -EINVAL.
133 */
134 size += iv->iov_len;
135 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
136 return XFS_ERROR(-EINVAL);
137 }
138 /* END copy & waste from filemap.c */
139
140 if (unlikely(ioflags & IO_ISDIRECT)) {
141 xfs_buftarg_t *target =
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100142 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 mp->m_rtdev_targp : mp->m_ddev_targp;
Nathan Scottce8e9222006-01-11 15:39:08 +1100144 if ((*offset & target->bt_smask) ||
145 (size & target->bt_smask)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000146 if (*offset == ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return (0);
148 }
149 return -XFS_ERROR(EINVAL);
150 }
151 }
152
153 n = XFS_MAXIOFFSET(mp) - *offset;
154 if ((n <= 0) || (size == 0))
155 return 0;
156
157 if (n < size)
158 size = n;
159
Nathan Scotta13828b2006-03-17 17:26:14 +1100160 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (unlikely(ioflags & IO_ISDIRECT))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800164 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 xfs_ilock(ip, XFS_IOLOCK_SHARED);
166
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000167 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000168 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
Christoph Hellwig126468b2008-03-06 13:44:57 +1100169 int iolock = XFS_IOLOCK_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100171 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *offset, size,
Christoph Hellwig126468b2008-03-06 13:44:57 +1100172 dmflags, &iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (ret) {
174 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000175 if (unlikely(ioflags & IO_ISDIRECT))
176 mutex_unlock(&inode->i_mutex);
177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 }
180
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000181 if (unlikely(ioflags & IO_ISDIRECT)) {
Christoph Hellwigdcf49cc2008-03-06 13:46:37 +1100182 if (inode->i_mapping->nrpages)
Dave Chinner2e656092008-11-28 14:23:33 +1100183 ret = -xfs_flushinval_pages(ip, (*offset & PAGE_CACHE_MASK),
Tim Shimmine6a4b372007-11-23 16:30:42 +1100184 -1, FI_REMAPF_LOCKED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000185 mutex_unlock(&inode->i_mutex);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000186 if (ret) {
187 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
188 return ret;
189 }
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000190 }
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000191
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000192 trace_xfs_file_read(ip, size, *offset, ioflags);
Badari Pulavarty543ade12006-09-30 23:28:48 -0700193
194 iocb->ki_pos = *offset;
195 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (ret > 0)
197 XFS_STATS_ADD(xs_read_bytes, ret);
198
199 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return ret;
201}
202
203ssize_t
Nathan Scott1b895842006-03-31 13:08:59 +1000204xfs_splice_read(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000205 xfs_inode_t *ip,
Nathan Scott1b895842006-03-31 13:08:59 +1000206 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200207 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200208 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000209 size_t count,
210 int flags,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000211 int ioflags)
Nathan Scott1b895842006-03-31 13:08:59 +1000212{
Nathan Scott1b895842006-03-31 13:08:59 +1000213 xfs_mount_t *mp = ip->i_mount;
214 ssize_t ret;
215
216 XFS_STATS_INC(xs_read_calls);
217 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
218 return -EIO;
219
220 xfs_ilock(ip, XFS_IOLOCK_SHARED);
221
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000222 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
Christoph Hellwig126468b2008-03-06 13:44:57 +1100223 int iolock = XFS_IOLOCK_SHARED;
Nathan Scott1b895842006-03-31 13:08:59 +1000224 int error;
225
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100226 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *ppos, count,
Christoph Hellwig126468b2008-03-06 13:44:57 +1100227 FILP_DELAY_FLAG(infilp), &iolock);
Nathan Scott1b895842006-03-31 13:08:59 +1000228 if (error) {
229 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
230 return -error;
231 }
232 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000233
234 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
235
Jens Axboecbb7e572006-04-11 14:57:50 +0200236 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000237 if (ret > 0)
238 XFS_STATS_ADD(xs_read_bytes, ret);
239
240 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
241 return ret;
242}
243
244ssize_t
245xfs_splice_write(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000246 xfs_inode_t *ip,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200247 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000248 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200249 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000250 size_t count,
251 int flags,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000252 int ioflags)
Nathan Scott1b895842006-03-31 13:08:59 +1000253{
Nathan Scott1b895842006-03-31 13:08:59 +1000254 xfs_mount_t *mp = ip->i_mount;
255 ssize_t ret;
David Chinner0a8d17d2006-09-07 14:27:15 +1000256 struct inode *inode = outfilp->f_mapping->host;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000257 xfs_fsize_t isize, new_size;
Nathan Scott1b895842006-03-31 13:08:59 +1000258
259 XFS_STATS_INC(xs_write_calls);
260 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
261 return -EIO;
262
263 xfs_ilock(ip, XFS_IOLOCK_EXCL);
264
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000265 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
Christoph Hellwig126468b2008-03-06 13:44:57 +1100266 int iolock = XFS_IOLOCK_EXCL;
Nathan Scott1b895842006-03-31 13:08:59 +1000267 int error;
268
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100269 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, *ppos, count,
Christoph Hellwig126468b2008-03-06 13:44:57 +1100270 FILP_DELAY_FLAG(outfilp), &iolock);
Nathan Scott1b895842006-03-31 13:08:59 +1000271 if (error) {
272 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
273 return -error;
274 }
275 }
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000276
277 new_size = *ppos + count;
278
279 xfs_ilock(ip, XFS_ILOCK_EXCL);
280 if (new_size > ip->i_size)
Christoph Hellwig613d7042007-10-11 17:44:08 +1000281 ip->i_new_size = new_size;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000282 xfs_iunlock(ip, XFS_ILOCK_EXCL);
283
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000284 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
285
Jens Axboecbb7e572006-04-11 14:57:50 +0200286 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000287 if (ret > 0)
288 XFS_STATS_ADD(xs_write_bytes, ret);
289
David Chinner0a8d17d2006-09-07 14:27:15 +1000290 isize = i_size_read(inode);
291 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
292 *ppos = isize;
293
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000294 if (*ppos > ip->i_size) {
David Chinner0a8d17d2006-09-07 14:27:15 +1000295 xfs_ilock(ip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000296 if (*ppos > ip->i_size)
297 ip->i_size = *ppos;
298 xfs_iunlock(ip, XFS_ILOCK_EXCL);
299 }
300
Christoph Hellwig613d7042007-10-11 17:44:08 +1000301 if (ip->i_new_size) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000302 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig613d7042007-10-11 17:44:08 +1000303 ip->i_new_size = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000304 if (ip->i_d.di_size > ip->i_size)
305 ip->i_d.di_size = ip->i_size;
David Chinner0a8d17d2006-09-07 14:27:15 +1000306 xfs_iunlock(ip, XFS_ILOCK_EXCL);
307 }
Nathan Scott1b895842006-03-31 13:08:59 +1000308 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return ret;
310}
311
312/*
313 * This routine is called to handle zeroing any space in the last
314 * block of the file that is beyond the EOF. We do this since the
315 * size is being increased without writing anything to that block
316 * and we don't want anyone to read the garbage on the disk.
317 */
318STATIC int /* error (positive) */
319xfs_zero_last_block(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000320 xfs_inode_t *ip,
Lachlan McIlroy68160162007-02-10 18:36:47 +1100321 xfs_fsize_t offset,
322 xfs_fsize_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 xfs_fileoff_t last_fsb;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000325 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int nimaps;
327 int zero_offset;
328 int zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int error = 0;
330 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000332 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Eric Sandeen24ee8082006-01-11 15:34:32 +1100334 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
335 if (zero_offset == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
337 * There are no extra bytes in the last block on disk to
338 * zero, so return.
339 */
340 return 0;
341 }
342
343 last_fsb = XFS_B_TO_FSBT(mp, isize);
344 nimaps = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000345 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000346 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (error) {
348 return error;
349 }
350 ASSERT(nimaps > 0);
351 /*
352 * If the block underlying isize is just a hole, then there
353 * is nothing to zero.
354 */
355 if (imap.br_startblock == HOLESTARTBLOCK) {
356 return 0;
357 }
358 /*
359 * Zero the part of the last block beyond the EOF, and write it
360 * out sync. We need to drop the ilock while we do this so we
361 * don't deadlock when the buffer cache calls back to us.
362 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000363 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Nathan Scott1b895842006-03-31 13:08:59 +1000364
Eric Sandeen24ee8082006-01-11 15:34:32 +1100365 zero_len = mp->m_sb.sb_blocksize - zero_offset;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100366 if (isize + zero_len > offset)
367 zero_len = offset - isize;
368 error = xfs_iozero(ip, isize, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000370 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ASSERT(error >= 0);
372 return error;
373}
374
375/*
376 * Zero any on disk space between the current EOF and the new,
377 * larger EOF. This handles the normal case of zeroing the remainder
378 * of the last block in the file and the unusual case of zeroing blocks
379 * out beyond the size of the file. This second case only happens
380 * with fixed size extents and when the system crashes before the inode
381 * size was updated but after blocks were allocated. If fill is set,
382 * then any holes in the range are filled and zeroed. If not, the holes
383 * are left alone as holes.
384 */
385
386int /* error (positive) */
387xfs_zero_eof(
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000388 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 xfs_off_t offset, /* starting I/O offset */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100390 xfs_fsize_t isize) /* current inode size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Christoph Hellwig613d7042007-10-11 17:44:08 +1000392 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 xfs_fileoff_t start_zero_fsb;
394 xfs_fileoff_t end_zero_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 xfs_fileoff_t zero_count_fsb;
396 xfs_fileoff_t last_fsb;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100397 xfs_fileoff_t zero_off;
398 xfs_fsize_t zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int nimaps;
400 int error = 0;
401 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000403 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Eric Sandeen24ee8082006-01-11 15:34:32 +1100404 ASSERT(offset > isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /*
407 * First handle zeroing the block on which isize resides.
408 * We only zero a part of that block so it is handled specially.
409 */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000410 error = xfs_zero_last_block(ip, offset, isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (error) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000412 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return error;
414 }
415
416 /*
417 * Calculate the range between the new size and the old
418 * where blocks needing to be zeroed may exist. To get the
419 * block where the last byte in the file currently resides,
420 * we need to subtract one from the size and truncate back
421 * to a block boundary. We subtract 1 in case the size is
422 * exactly on a block boundary.
423 */
424 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
425 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
426 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
427 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
428 if (last_fsb == end_zero_fsb) {
429 /*
430 * The size was only incremented on its last block.
431 * We took care of that above, so just return.
432 */
433 return 0;
434 }
435
436 ASSERT(start_zero_fsb <= end_zero_fsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 while (start_zero_fsb <= end_zero_fsb) {
438 nimaps = 1;
439 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000440 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000441 0, NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (error) {
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000443 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return error;
445 }
446 ASSERT(nimaps > 0);
447
448 if (imap.br_state == XFS_EXT_UNWRITTEN ||
449 imap.br_startblock == HOLESTARTBLOCK) {
450 /*
451 * This loop handles initializing pages that were
452 * partially initialized by the code below this
453 * loop. It basically zeroes the part of the page
454 * that sits on a hole and sets the page as P_HOLE
455 * and calls remapf if it is a mapped file.
456 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100457 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
459 continue;
460 }
461
462 /*
Nathan Scott3d80ede2006-06-09 14:57:30 +1000463 * There are blocks we need to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * Drop the inode lock while we're doing the I/O.
465 * We'll still have the iolock to protect us.
466 */
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000467 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Lachlan McIlroy68160162007-02-10 18:36:47 +1100469 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
470 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
471
472 if ((zero_off + zero_len) > offset)
473 zero_len = offset - zero_off;
474
475 error = xfs_iozero(ip, zero_off, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (error) {
477 goto out_lock;
478 }
479
Nathan Scott3d80ede2006-06-09 14:57:30 +1000480 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
482
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000483 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
486 return 0;
487
488out_lock:
Christoph Hellwig579aa9c2008-04-22 17:34:00 +1000489 xfs_ilock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ASSERT(error >= 0);
491 return error;
492}
493
494ssize_t /* bytes written, or (-) error */
495xfs_write(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000496 struct xfs_inode *xip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct kiocb *iocb,
498 const struct iovec *iovp,
499 unsigned int nsegs,
500 loff_t *offset,
Christoph Hellwig993386c2007-08-28 16:12:30 +1000501 int ioflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct file *file = iocb->ki_filp;
504 struct address_space *mapping = file->f_mapping;
505 struct inode *inode = mapping->host;
506 unsigned long segs = nsegs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 xfs_mount_t *mp;
508 ssize_t ret = 0, error = 0;
509 xfs_fsize_t isize, new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int iolock;
511 int eventsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 size_t ocount = 0, count;
513 loff_t pos;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000514 int need_i_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 XFS_STATS_INC(xs_write_calls);
517
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -0700518 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
519 if (error)
520 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 count = ocount;
523 pos = *offset;
524
525 if (count == 0)
526 return 0;
527
Christoph Hellwig613d7042007-10-11 17:44:08 +1000528 mp = xip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000530 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
Nathan Scott34327e12006-06-09 17:11:55 +1000531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (XFS_FORCED_SHUTDOWN(mp))
533 return -EIO;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535relock:
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000536 if (ioflags & IO_ISDIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 iolock = XFS_IOLOCK_SHARED;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000538 need_i_mutex = 0;
539 } else {
540 iolock = XFS_IOLOCK_EXCL;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000541 need_i_mutex = 1;
542 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
545 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547start:
548 error = -generic_write_checks(file, &pos, &count,
549 S_ISBLK(inode->i_mode));
550 if (error) {
551 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100552 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000555 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 !(ioflags & IO_INVIS) && !eventsent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int dmflags = FILP_DELAY_FLAG(file);
558
Nathan Scotta13828b2006-03-17 17:26:14 +1100559 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800560 dmflags |= DM_FLAGS_IMUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 xfs_iunlock(xip, XFS_ILOCK_EXCL);
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100563 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, xip,
Christoph Hellwig126468b2008-03-06 13:44:57 +1100564 pos, count, dmflags, &iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (error) {
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000566 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 xfs_ilock(xip, XFS_ILOCK_EXCL);
569 eventsent = 1;
570
571 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000572 * The iolock was dropped and reacquired in XFS_SEND_DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 * so we have to recheck the size when appending.
574 * We will only "goto start;" once, since having sent the
575 * event prevents another call to XFS_SEND_DATA, which is
576 * what allows the size to change in the first place.
577 */
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000578 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
579 goto start;
580 }
581
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000582 if (ioflags & IO_ISDIRECT) {
583 xfs_buftarg_t *target =
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100584 XFS_IS_REALTIME_INODE(xip) ?
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000585 mp->m_rtdev_targp : mp->m_ddev_targp;
586
587 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
588 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
589 return XFS_ERROR(-EINVAL);
590 }
591
Christoph Hellwigdcf49cc2008-03-06 13:46:37 +1100592 if (!need_i_mutex && (mapping->nrpages || pos > xip->i_size)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000593 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
594 iolock = XFS_IOLOCK_EXCL;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000595 need_i_mutex = 1;
596 mutex_lock(&inode->i_mutex);
597 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 goto start;
599 }
600 }
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 new_size = pos + count;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000603 if (new_size > xip->i_size)
Christoph Hellwig613d7042007-10-11 17:44:08 +1000604 xip->i_new_size = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Christoph Hellwig2234d542008-12-03 12:20:22 +0100606 if (likely(!(ioflags & IO_INVIS)))
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000607 file_update_time(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /*
610 * If the offset is beyond the size of the file, we have a couple
611 * of things to do. First, if there is already space allocated
612 * we need to either create holes or zero the disk or ...
613 *
614 * If there is a page where the previous size lands, we need
615 * to zero it out up to the new size.
616 */
617
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000618 if (pos > xip->i_size) {
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000619 error = xfs_zero_eof(xip, pos, xip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (error) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000621 xfs_iunlock(xip, XFS_ILOCK_EXCL);
622 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 }
625 xfs_iunlock(xip, XFS_ILOCK_EXCL);
626
627 /*
628 * If we're writing the file then make sure to clear the
629 * setuid and setgid bits if the process is not being run
630 * by root. This keeps people from modifying setuid and
631 * setgid binaries.
632 */
Christoph Hellwig87185512010-02-03 19:43:31 +0000633 error = -file_remove_suid(file);
634 if (unlikely(error))
635 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* We can write back this queue in page reclaim */
638 current->backing_dev_info = mapping->backing_dev_info;
639
640 if ((ioflags & IO_ISDIRECT)) {
Christoph Hellwigdcf49cc2008-03-06 13:46:37 +1100641 if (mapping->nrpages) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000642 WARN_ON(need_i_mutex == 0);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000643 error = xfs_flushinval_pages(xip,
Tim Shimmine6a4b372007-11-23 16:30:42 +1100644 (pos & PAGE_CACHE_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000646 if (error)
647 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
Nathan Scotta13828b2006-03-17 17:26:14 +1100650 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* demote the lock now the cached pages are gone */
Lachlan McIlroy541d7d32007-10-11 17:34:33 +1000652 xfs_ilock_demote(xip, XFS_IOLOCK_EXCL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800653 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 iolock = XFS_IOLOCK_SHARED;
Nathan Scotta13828b2006-03-17 17:26:14 +1100656 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000659 trace_xfs_file_direct_write(xip, count, *offset, ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 ret = generic_file_direct_write(iocb, iovp,
661 &segs, pos, offset, count, ocount);
662
663 /*
664 * direct-io write to a hole: fall through to buffered I/O
665 * for completing the rest of the request.
666 */
667 if (ret >= 0 && ret != count) {
668 XFS_STATS_ADD(xs_write_bytes, ret);
669
670 pos += ret;
671 count -= ret;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 ioflags &= ~IO_ISDIRECT;
674 xfs_iunlock(xip, iolock);
675 goto relock;
676 }
677 } else {
Dave Chinner58252942009-04-06 18:45:44 +0200678 int enospc = 0;
679 ssize_t ret2 = 0;
680
681write_retry:
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000682 trace_xfs_file_buffered_write(xip, count, *offset, ioflags);
Dave Chinner58252942009-04-06 18:45:44 +0200683 ret2 = generic_file_buffered_write(iocb, iovp, segs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 pos, offset, count, ret);
Dave Chinner58252942009-04-06 18:45:44 +0200685 /*
686 * if we just got an ENOSPC, flush the inode now we
687 * aren't holding any page locks and retry *once*
688 */
689 if (ret2 == -ENOSPC && !enospc) {
690 error = xfs_flush_pages(xip, 0, -1, 0, FI_NONE);
691 if (error)
692 goto out_unlock_internal;
693 enospc = 1;
694 goto write_retry;
695 }
696 ret = ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 current->backing_dev_info = NULL;
700
Lachlan McIlroy25051152008-12-24 14:07:32 +1100701 isize = i_size_read(inode);
702 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
703 *offset = isize;
704
705 if (*offset > xip->i_size) {
706 xfs_ilock(xip, XFS_ILOCK_EXCL);
707 if (*offset > xip->i_size)
708 xip->i_size = *offset;
709 xfs_iunlock(xip, XFS_ILOCK_EXCL);
710 }
711
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000712 if (ret == -ENOSPC &&
713 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
Christoph Hellwig126468b2008-03-06 13:44:57 +1100714 xfs_iunlock(xip, iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100715 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800716 mutex_unlock(&inode->i_mutex);
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100717 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, xip,
718 DM_RIGHT_NULL, xip, DM_RIGHT_NULL, NULL, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 0, 0, 0); /* Delay flag intentionally unused */
Nathan Scotta13828b2006-03-17 17:26:14 +1100720 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800721 mutex_lock(&inode->i_mutex);
Christoph Hellwig126468b2008-03-06 13:44:57 +1100722 xfs_ilock(xip, iolock);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000723 if (error)
724 goto out_unlock_internal;
Lachlan McIlroy25051152008-12-24 14:07:32 +1100725 goto start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
728 error = -ret;
729 if (ret <= 0)
730 goto out_unlock_internal;
731
732 XFS_STATS_ADD(xs_write_bytes, ret);
733
734 /* Handle various SYNC-type writes */
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100735 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300736 loff_t end = pos + ret - 1;
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000737 int error2;
Christoph Hellwig126468b2008-03-06 13:44:57 +1100738
739 xfs_iunlock(xip, iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100740 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800741 mutex_unlock(&inode->i_mutex);
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300742
743 error2 = filemap_write_and_wait_range(mapping, pos, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!error)
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000745 error = error2;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000746 if (need_i_mutex)
747 mutex_lock(&inode->i_mutex);
Christoph Hellwig126468b2008-03-06 13:44:57 +1100748 xfs_ilock(xip, iolock);
Christoph Hellwig13e6d5c2009-08-31 21:00:31 -0300749
750 error2 = xfs_fsync(xip);
Lachlan McIlroy5903c492007-09-14 15:22:08 +1000751 if (!error)
752 error = error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 out_unlock_internal:
Christoph Hellwig613d7042007-10-11 17:44:08 +1000756 if (xip->i_new_size) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000757 xfs_ilock(xip, XFS_ILOCK_EXCL);
Christoph Hellwig613d7042007-10-11 17:44:08 +1000758 xip->i_new_size = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000759 /*
760 * If this was a direct or synchronous I/O that failed (such
761 * as ENOSPC) then part of the I/O may have been written to
762 * disk before the error occured. In this case the on-disk
763 * file size may have been adjusted beyond the in-memory file
764 * size and now needs to be truncated back.
765 */
766 if (xip->i_d.di_size > xip->i_size)
767 xip->i_d.di_size = xip->i_size;
768 xfs_iunlock(xip, XFS_ILOCK_EXCL);
769 }
Christoph Hellwig126468b2008-03-06 13:44:57 +1100770 xfs_iunlock(xip, iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100771 out_unlock_mutex:
772 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800773 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -error;
775}
776
777/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * If the underlying (data/log/rt) device is readonly, there are some
779 * operations that cannot proceed.
780 */
781int
782xfs_dev_is_read_only(
783 xfs_mount_t *mp,
784 char *message)
785{
786 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
787 xfs_readonly_buftarg(mp->m_logdev_targp) ||
788 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
789 cmn_err(CE_NOTE,
790 "XFS: %s required on read-only device.", message);
791 cmn_err(CE_NOTE,
792 "XFS: write access unavailable, cannot proceed.");
793 return EROFS;
794 }
795 return 0;
796}