blob: ed90403f0ee71dcfaefc4ae03d84990d59155f3e [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"
51
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,
60 xfs_iocore_t *io,
61 void *data,
62 size_t segs,
63 loff_t offset,
64 int ioflags)
65{
66 xfs_inode_t *ip = XFS_IO_INODE(io);
67
68 if (ip->i_rwtrace == NULL)
69 return;
70 ktrace_enter(ip->i_rwtrace,
71 (void *)(unsigned long)tag,
72 (void *)ip,
73 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
74 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
75 (void *)data,
76 (void *)((unsigned long)segs),
77 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
78 (void *)((unsigned long)(offset & 0xffffffff)),
79 (void *)((unsigned long)ioflags),
80 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
81 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +110082 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 (void *)NULL,
84 (void *)NULL,
85 (void *)NULL,
86 (void *)NULL);
87}
88
89void
90xfs_inval_cached_trace(
91 xfs_iocore_t *io,
92 xfs_off_t offset,
93 xfs_off_t len,
94 xfs_off_t first,
95 xfs_off_t last)
96{
97 xfs_inode_t *ip = XFS_IO_INODE(io);
98
99 if (ip->i_rwtrace == NULL)
100 return;
101 ktrace_enter(ip->i_rwtrace,
102 (void *)(__psint_t)XFS_INVAL_CACHED,
103 (void *)ip,
104 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(offset & 0xffffffff)),
106 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
107 (void *)((unsigned long)(len & 0xffffffff)),
108 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
109 (void *)((unsigned long)(first & 0xffffffff)),
110 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
111 (void *)((unsigned long)(last & 0xffffffff)),
Yingping Luf1fdc842006-03-22 12:44:15 +1100112 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 (void *)NULL,
114 (void *)NULL,
115 (void *)NULL,
116 (void *)NULL,
117 (void *)NULL);
118}
119#endif
120
121/*
122 * xfs_iozero
123 *
124 * xfs_iozero clears the specified range of buffer supplied,
125 * and marks all the affected blocks as valid and modified. If
126 * an affected block is not allocated, it will be allocated. If
127 * an affected block is not completely overwritten, and is not
128 * valid before the operation, it will be read from disk before
129 * being partially zeroed.
130 */
131STATIC int
132xfs_iozero(
133 struct inode *ip, /* inode */
134 loff_t pos, /* offset in file */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100135 size_t count) /* size of data to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned bytes;
138 struct page *page;
139 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int status;
141
142 mapping = ip->i_mapping;
143 do {
144 unsigned long index, offset;
145
146 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
147 index = pos >> PAGE_CACHE_SHIFT;
148 bytes = PAGE_CACHE_SIZE - offset;
149 if (bytes > count)
150 bytes = count;
151
152 status = -ENOMEM;
153 page = grab_cache_page(mapping, index);
154 if (!page)
155 break;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 status = mapping->a_ops->prepare_write(NULL, page, offset,
158 offset + bytes);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100159 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Christoph Hellwig700716c2007-05-24 15:27:17 +1000162 zero_user_page(page, offset, bytes, KM_USER0);
David Chinnere7ff6ae2007-02-10 18:37:46 +1100163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 status = mapping->a_ops->commit_write(NULL, page, offset,
165 offset + bytes);
166 if (!status) {
167 pos += bytes;
168 count -= bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
171unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unlock_page(page);
173 page_cache_release(page);
174 if (status)
175 break;
176 } while (count);
177
178 return (-status);
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181ssize_t /* bytes read, or (-) error */
182xfs_read(
183 bhv_desc_t *bdp,
184 struct kiocb *iocb,
185 const struct iovec *iovp,
186 unsigned int segs,
187 loff_t *offset,
188 int ioflags,
189 cred_t *credp)
190{
191 struct file *file = iocb->ki_filp;
192 struct inode *inode = file->f_mapping->host;
193 size_t size = 0;
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000194 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 xfs_fsize_t n;
196 xfs_inode_t *ip;
197 xfs_mount_t *mp;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000198 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned long seg;
200
201 ip = XFS_BHVTOI(bdp);
202 vp = BHV_TO_VNODE(bdp);
203 mp = ip->i_mount;
204
205 XFS_STATS_INC(xs_read_calls);
206
207 /* START copy & waste from filemap.c */
208 for (seg = 0; seg < segs; seg++) {
209 const struct iovec *iv = &iovp[seg];
210
211 /*
212 * If any segment has a negative length, or the cumulative
213 * length ever wraps negative then return -EINVAL.
214 */
215 size += iv->iov_len;
216 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
217 return XFS_ERROR(-EINVAL);
218 }
219 /* END copy & waste from filemap.c */
220
221 if (unlikely(ioflags & IO_ISDIRECT)) {
222 xfs_buftarg_t *target =
223 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
224 mp->m_rtdev_targp : mp->m_ddev_targp;
Nathan Scottce8e9222006-01-11 15:39:08 +1100225 if ((*offset & target->bt_smask) ||
226 (size & target->bt_smask)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000227 if (*offset == ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return (0);
229 }
230 return -XFS_ERROR(EINVAL);
231 }
232 }
233
234 n = XFS_MAXIOFFSET(mp) - *offset;
235 if ((n <= 0) || (size == 0))
236 return 0;
237
238 if (n < size)
239 size = n;
240
Nathan Scotta13828b2006-03-17 17:26:14 +1100241 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 if (unlikely(ioflags & IO_ISDIRECT))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800245 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 xfs_ilock(ip, XFS_IOLOCK_SHARED);
247
248 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
249 !(ioflags & IO_INVIS)) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000250 bhv_vrwlock_t locktype = VRWLOCK_READ;
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000251 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ,
254 BHV_TO_VNODE(bdp), *offset, size,
Dean Roehriche1a40fa2005-06-22 10:20:44 +1000255 dmflags, &locktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (ret) {
257 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000258 if (unlikely(ioflags & IO_ISDIRECT))
259 mutex_unlock(&inode->i_mutex);
260 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 }
263
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000264 if (unlikely(ioflags & IO_ISDIRECT)) {
265 if (VN_CACHED(vp))
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000266 ret = bhv_vop_flushinval_pages(vp, ctooff(offtoct(*offset)),
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000267 -1, FI_REMAPF_LOCKED);
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000268 mutex_unlock(&inode->i_mutex);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000269 if (ret) {
270 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
271 return ret;
272 }
Nathan Scottedcd4bc2006-09-28 11:05:33 +1000273 }
Lachlan McIlroy721259b2006-09-07 14:27:05 +1000274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
276 (void *)iovp, segs, *offset, ioflags);
Badari Pulavarty543ade12006-09-30 23:28:48 -0700277
278 iocb->ki_pos = *offset;
279 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
281 ret = wait_on_sync_kiocb(iocb);
282 if (ret > 0)
283 XFS_STATS_ADD(xs_read_bytes, ret);
284
285 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return ret;
287}
288
289ssize_t
290xfs_sendfile(
291 bhv_desc_t *bdp,
292 struct file *filp,
293 loff_t *offset,
294 int ioflags,
295 size_t count,
296 read_actor_t actor,
297 void *target,
298 cred_t *credp)
299{
Nathan Scott1b895842006-03-31 13:08:59 +1000300 xfs_inode_t *ip = XFS_BHVTOI(bdp);
301 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 XFS_STATS_INC(xs_read_calls);
Nathan Scott1b895842006-03-31 13:08:59 +1000305 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return -EIO;
307
308 xfs_ilock(ip, XFS_IOLOCK_SHARED);
309
Nathan Scott1b895842006-03-31 13:08:59 +1000310 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 (!(ioflags & IO_INVIS))) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000312 bhv_vrwlock_t locktype = VRWLOCK_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int error;
314
Nathan Scott1b895842006-03-31 13:08:59 +1000315 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
316 *offset, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 FILP_DELAY_FLAG(filp), &locktype);
318 if (error) {
319 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
320 return -error;
321 }
322 }
323 xfs_rw_enter_trace(XFS_SENDFILE_ENTER, &ip->i_iocore,
324 (void *)(unsigned long)target, count, *offset, ioflags);
325 ret = generic_file_sendfile(filp, offset, count, actor, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (ret > 0)
327 XFS_STATS_ADD(xs_read_bytes, ret);
328
Nathan Scott1b895842006-03-31 13:08:59 +1000329 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
330 return ret;
331}
332
333ssize_t
334xfs_splice_read(
335 bhv_desc_t *bdp,
336 struct file *infilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200337 loff_t *ppos,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200338 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000339 size_t count,
340 int flags,
341 int ioflags,
342 cred_t *credp)
343{
344 xfs_inode_t *ip = XFS_BHVTOI(bdp);
345 xfs_mount_t *mp = ip->i_mount;
346 ssize_t ret;
347
348 XFS_STATS_INC(xs_read_calls);
349 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
350 return -EIO;
351
352 xfs_ilock(ip, XFS_IOLOCK_SHARED);
353
354 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
355 (!(ioflags & IO_INVIS))) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000356 bhv_vrwlock_t locktype = VRWLOCK_READ;
Nathan Scott1b895842006-03-31 13:08:59 +1000357 int error;
358
359 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
Jens Axboecbb7e572006-04-11 14:57:50 +0200360 *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000361 FILP_DELAY_FLAG(infilp), &locktype);
362 if (error) {
363 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
364 return -error;
365 }
366 }
367 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
Jens Axboecbb7e572006-04-11 14:57:50 +0200368 pipe, count, *ppos, ioflags);
369 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000370 if (ret > 0)
371 XFS_STATS_ADD(xs_read_bytes, ret);
372
373 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
374 return ret;
375}
376
377ssize_t
378xfs_splice_write(
379 bhv_desc_t *bdp,
Ingo Molnar3a326a22006-04-10 15:18:35 +0200380 struct pipe_inode_info *pipe,
Nathan Scott1b895842006-03-31 13:08:59 +1000381 struct file *outfilp,
Jens Axboecbb7e572006-04-11 14:57:50 +0200382 loff_t *ppos,
Nathan Scott1b895842006-03-31 13:08:59 +1000383 size_t count,
384 int flags,
385 int ioflags,
386 cred_t *credp)
387{
388 xfs_inode_t *ip = XFS_BHVTOI(bdp);
389 xfs_mount_t *mp = ip->i_mount;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000390 xfs_iocore_t *io = &ip->i_iocore;
Nathan Scott1b895842006-03-31 13:08:59 +1000391 ssize_t ret;
David Chinner0a8d17d2006-09-07 14:27:15 +1000392 struct inode *inode = outfilp->f_mapping->host;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000393 xfs_fsize_t isize, new_size;
Nathan Scott1b895842006-03-31 13:08:59 +1000394
395 XFS_STATS_INC(xs_write_calls);
396 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
397 return -EIO;
398
399 xfs_ilock(ip, XFS_IOLOCK_EXCL);
400
401 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_WRITE) &&
402 (!(ioflags & IO_INVIS))) {
Nathan Scott8285fb52006-06-09 17:07:12 +1000403 bhv_vrwlock_t locktype = VRWLOCK_WRITE;
Nathan Scott1b895842006-03-31 13:08:59 +1000404 int error;
405
406 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, BHV_TO_VNODE(bdp),
Jens Axboecbb7e572006-04-11 14:57:50 +0200407 *ppos, count,
Nathan Scott1b895842006-03-31 13:08:59 +1000408 FILP_DELAY_FLAG(outfilp), &locktype);
409 if (error) {
410 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
411 return -error;
412 }
413 }
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000414
415 new_size = *ppos + count;
416
417 xfs_ilock(ip, XFS_ILOCK_EXCL);
418 if (new_size > ip->i_size)
419 io->io_new_size = new_size;
420 xfs_iunlock(ip, XFS_ILOCK_EXCL);
421
Nathan Scott1b895842006-03-31 13:08:59 +1000422 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore,
Jens Axboecbb7e572006-04-11 14:57:50 +0200423 pipe, count, *ppos, ioflags);
424 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
Nathan Scott1b895842006-03-31 13:08:59 +1000425 if (ret > 0)
426 XFS_STATS_ADD(xs_write_bytes, ret);
427
David Chinner0a8d17d2006-09-07 14:27:15 +1000428 isize = i_size_read(inode);
429 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
430 *ppos = isize;
431
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000432 if (*ppos > ip->i_size) {
David Chinner0a8d17d2006-09-07 14:27:15 +1000433 xfs_ilock(ip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000434 if (*ppos > ip->i_size)
435 ip->i_size = *ppos;
436 xfs_iunlock(ip, XFS_ILOCK_EXCL);
437 }
438
439 if (io->io_new_size) {
440 xfs_ilock(ip, XFS_ILOCK_EXCL);
441 io->io_new_size = 0;
442 if (ip->i_d.di_size > ip->i_size)
443 ip->i_d.di_size = ip->i_size;
David Chinner0a8d17d2006-09-07 14:27:15 +1000444 xfs_iunlock(ip, XFS_ILOCK_EXCL);
445 }
Nathan Scott1b895842006-03-31 13:08:59 +1000446 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return ret;
448}
449
450/*
451 * This routine is called to handle zeroing any space in the last
452 * block of the file that is beyond the EOF. We do this since the
453 * size is being increased without writing anything to that block
454 * and we don't want anyone to read the garbage on the disk.
455 */
456STATIC int /* error (positive) */
457xfs_zero_last_block(
458 struct inode *ip,
459 xfs_iocore_t *io,
Lachlan McIlroy68160162007-02-10 18:36:47 +1100460 xfs_fsize_t offset,
461 xfs_fsize_t isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 xfs_fileoff_t last_fsb;
Nathan Scott1b895842006-03-31 13:08:59 +1000464 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int nimaps;
466 int zero_offset;
467 int zero_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int error = 0;
469 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Eric Sandeen24ee8082006-01-11 15:34:32 +1100473 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
474 if (zero_offset == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
476 * There are no extra bytes in the last block on disk to
477 * zero, so return.
478 */
479 return 0;
480 }
481
482 last_fsb = XFS_B_TO_FSBT(mp, isize);
483 nimaps = 1;
484 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000485 &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (error) {
487 return error;
488 }
489 ASSERT(nimaps > 0);
490 /*
491 * If the block underlying isize is just a hole, then there
492 * is nothing to zero.
493 */
494 if (imap.br_startblock == HOLESTARTBLOCK) {
495 return 0;
496 }
497 /*
498 * Zero the part of the last block beyond the EOF, and write it
499 * out sync. We need to drop the ilock while we do this so we
500 * don't deadlock when the buffer cache calls back to us.
501 */
502 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
Nathan Scott1b895842006-03-31 13:08:59 +1000503
Eric Sandeen24ee8082006-01-11 15:34:32 +1100504 zero_len = mp->m_sb.sb_blocksize - zero_offset;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100505 if (isize + zero_len > offset)
506 zero_len = offset - isize;
507 error = xfs_iozero(ip, isize, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
510 ASSERT(error >= 0);
511 return error;
512}
513
514/*
515 * Zero any on disk space between the current EOF and the new,
516 * larger EOF. This handles the normal case of zeroing the remainder
517 * of the last block in the file and the unusual case of zeroing blocks
518 * out beyond the size of the file. This second case only happens
519 * with fixed size extents and when the system crashes before the inode
520 * size was updated but after blocks were allocated. If fill is set,
521 * then any holes in the range are filled and zeroed. If not, the holes
522 * are left alone as holes.
523 */
524
525int /* error (positive) */
526xfs_zero_eof(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000527 bhv_vnode_t *vp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 xfs_iocore_t *io,
529 xfs_off_t offset, /* starting I/O offset */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100530 xfs_fsize_t isize) /* current inode size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Nathan Scottec86dc02006-03-17 17:25:36 +1100532 struct inode *ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 xfs_fileoff_t start_zero_fsb;
534 xfs_fileoff_t end_zero_fsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 xfs_fileoff_t zero_count_fsb;
536 xfs_fileoff_t last_fsb;
Lachlan McIlroy68160162007-02-10 18:36:47 +1100537 xfs_fileoff_t zero_off;
538 xfs_fsize_t zero_len;
Nathan Scott1b895842006-03-31 13:08:59 +1000539 xfs_mount_t *mp = io->io_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 int nimaps;
541 int error = 0;
542 xfs_bmbt_irec_t imap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
545 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
Eric Sandeen24ee8082006-01-11 15:34:32 +1100546 ASSERT(offset > isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /*
549 * First handle zeroing the block on which isize resides.
550 * We only zero a part of that block so it is handled specially.
551 */
Lachlan McIlroy68160162007-02-10 18:36:47 +1100552 error = xfs_zero_last_block(ip, io, offset, isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (error) {
554 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
555 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
556 return error;
557 }
558
559 /*
560 * Calculate the range between the new size and the old
561 * where blocks needing to be zeroed may exist. To get the
562 * block where the last byte in the file currently resides,
563 * we need to subtract one from the size and truncate back
564 * to a block boundary. We subtract 1 in case the size is
565 * exactly on a block boundary.
566 */
567 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
568 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
569 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
570 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
571 if (last_fsb == end_zero_fsb) {
572 /*
573 * The size was only incremented on its last block.
574 * We took care of that above, so just return.
575 */
576 return 0;
577 }
578
579 ASSERT(start_zero_fsb <= end_zero_fsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 while (start_zero_fsb <= end_zero_fsb) {
581 nimaps = 1;
582 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
583 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000584 0, NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (error) {
586 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
587 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
588 return error;
589 }
590 ASSERT(nimaps > 0);
591
592 if (imap.br_state == XFS_EXT_UNWRITTEN ||
593 imap.br_startblock == HOLESTARTBLOCK) {
594 /*
595 * This loop handles initializing pages that were
596 * partially initialized by the code below this
597 * loop. It basically zeroes the part of the page
598 * that sits on a hole and sets the page as P_HOLE
599 * and calls remapf if it is a mapped file.
600 */
Eric Sandeen24ee8082006-01-11 15:34:32 +1100601 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
603 continue;
604 }
605
606 /*
Nathan Scott3d80ede2006-06-09 14:57:30 +1000607 * There are blocks we need to zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * Drop the inode lock while we're doing the I/O.
609 * We'll still have the iolock to protect us.
610 */
611 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
612
Lachlan McIlroy68160162007-02-10 18:36:47 +1100613 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
614 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
615
616 if ((zero_off + zero_len) > offset)
617 zero_len = offset - zero_off;
618
619 error = xfs_iozero(ip, zero_off, zero_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (error) {
621 goto out_lock;
622 }
623
Nathan Scott3d80ede2006-06-09 14:57:30 +1000624 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
626
627 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
628 }
629
630 return 0;
631
632out_lock:
633
634 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
635 ASSERT(error >= 0);
636 return error;
637}
638
639ssize_t /* bytes written, or (-) error */
640xfs_write(
641 bhv_desc_t *bdp,
642 struct kiocb *iocb,
643 const struct iovec *iovp,
644 unsigned int nsegs,
645 loff_t *offset,
646 int ioflags,
647 cred_t *credp)
648{
649 struct file *file = iocb->ki_filp;
650 struct address_space *mapping = file->f_mapping;
651 struct inode *inode = mapping->host;
652 unsigned long segs = nsegs;
653 xfs_inode_t *xip;
654 xfs_mount_t *mp;
655 ssize_t ret = 0, error = 0;
656 xfs_fsize_t isize, new_size;
657 xfs_iocore_t *io;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000658 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int iolock;
660 int eventsent = 0;
Nathan Scott8285fb52006-06-09 17:07:12 +1000661 bhv_vrwlock_t locktype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 size_t ocount = 0, count;
663 loff_t pos;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000664 int need_i_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 XFS_STATS_INC(xs_write_calls);
667
668 vp = BHV_TO_VNODE(bdp);
669 xip = XFS_BHVTOI(bdp);
670
Dmitriy Monakhov0ceb3312007-05-08 00:23:02 -0700671 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
672 if (error)
673 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 count = ocount;
676 pos = *offset;
677
678 if (count == 0)
679 return 0;
680
681 io = &xip->i_iocore;
682 mp = io->io_mount;
683
Nathan Scott34327e12006-06-09 17:11:55 +1000684 vfs_wait_for_freeze(vp->v_vfsp, SB_FREEZE_WRITE);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (XFS_FORCED_SHUTDOWN(mp))
687 return -EIO;
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689relock:
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000690 if (ioflags & IO_ISDIRECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 iolock = XFS_IOLOCK_SHARED;
692 locktype = VRWLOCK_WRITE_DIRECT;
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000693 need_i_mutex = 0;
694 } else {
695 iolock = XFS_IOLOCK_EXCL;
696 locktype = VRWLOCK_WRITE;
697 need_i_mutex = 1;
698 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703start:
704 error = -generic_write_checks(file, &pos, &count,
705 S_ISBLK(inode->i_mode));
706 if (error) {
707 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
Nathan Scotta13828b2006-03-17 17:26:14 +1100708 goto out_unlock_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
712 !(ioflags & IO_INVIS) && !eventsent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int dmflags = FILP_DELAY_FLAG(file);
714
Nathan Scotta13828b2006-03-17 17:26:14 +1100715 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800716 dmflags |= DM_FLAGS_IMUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 xfs_iunlock(xip, XFS_ILOCK_EXCL);
719 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
720 pos, count,
721 dmflags, &locktype);
722 if (error) {
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000723 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 xfs_ilock(xip, XFS_ILOCK_EXCL);
726 eventsent = 1;
727
728 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000729 * The iolock was dropped and reacquired in XFS_SEND_DATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * so we have to recheck the size when appending.
731 * We will only "goto start;" once, since having sent the
732 * event prevents another call to XFS_SEND_DATA, which is
733 * what allows the size to change in the first place.
734 */
Lachlan McIlroy71dfd5a2007-05-08 13:50:12 +1000735 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
736 goto start;
737 }
738
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000739 if (ioflags & IO_ISDIRECT) {
740 xfs_buftarg_t *target =
741 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
742 mp->m_rtdev_targp : mp->m_ddev_targp;
743
744 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
745 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
746 return XFS_ERROR(-EINVAL);
747 }
748
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000749 if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000750 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
751 iolock = XFS_IOLOCK_EXCL;
752 locktype = VRWLOCK_WRITE;
753 need_i_mutex = 1;
754 mutex_lock(&inode->i_mutex);
755 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 goto start;
757 }
758 }
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 new_size = pos + count;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000761 if (new_size > xip->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 io->io_new_size = new_size;
763
Nathan Scott4aeb6642005-11-02 11:43:58 +1100764 if (likely(!(ioflags & IO_INVIS))) {
Christoph Hellwig870f4812006-01-09 20:52:01 -0800765 file_update_time(file);
Nathan Scott4aeb6642005-11-02 11:43:58 +1100766 xfs_ichgtime_fast(xip, inode,
767 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
770 /*
771 * If the offset is beyond the size of the file, we have a couple
772 * of things to do. First, if there is already space allocated
773 * we need to either create holes or zero the disk or ...
774 *
775 * If there is a page where the previous size lands, we need
776 * to zero it out up to the new size.
777 */
778
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000779 if (pos > xip->i_size) {
780 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos, xip->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (error) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000782 xfs_iunlock(xip, XFS_ILOCK_EXCL);
783 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785 }
786 xfs_iunlock(xip, XFS_ILOCK_EXCL);
787
788 /*
789 * If we're writing the file then make sure to clear the
790 * setuid and setgid bits if the process is not being run
791 * by root. This keeps people from modifying setuid and
792 * setgid binaries.
793 */
794
795 if (((xip->i_d.di_mode & S_ISUID) ||
796 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
797 (S_ISGID | S_IXGRP))) &&
798 !capable(CAP_FSETID)) {
799 error = xfs_write_clear_setuid(xip);
800 if (likely(!error))
Josef "Jeff" Sipeke678fb02006-12-08 02:36:49 -0800801 error = -remove_suid(file->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (unlikely(error)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000803 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 }
806
807retry:
808 /* We can write back this queue in page reclaim */
809 current->backing_dev_info = mapping->backing_dev_info;
810
811 if ((ioflags & IO_ISDIRECT)) {
Lachlan McIlroy2a329632007-05-08 13:49:39 +1000812 if (VN_CACHED(vp)) {
813 WARN_ON(need_i_mutex == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 xfs_inval_cached_trace(io, pos, -1,
815 ctooff(offtoct(pos)), -1);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000816 error = bhv_vop_flushinval_pages(vp, ctooff(offtoct(pos)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +1000818 if (error)
819 goto out_unlock_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Nathan Scotta13828b2006-03-17 17:26:14 +1100822 if (need_i_mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* demote the lock now the cached pages are gone */
824 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800825 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 iolock = XFS_IOLOCK_SHARED;
828 locktype = VRWLOCK_WRITE_DIRECT;
Nathan Scotta13828b2006-03-17 17:26:14 +1100829 need_i_mutex = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
833 *offset, ioflags);
834 ret = generic_file_direct_write(iocb, iovp,
835 &segs, pos, offset, count, ocount);
836
837 /*
838 * direct-io write to a hole: fall through to buffered I/O
839 * for completing the rest of the request.
840 */
841 if (ret >= 0 && ret != count) {
842 XFS_STATS_ADD(xs_write_bytes, ret);
843
844 pos += ret;
845 count -= ret;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 ioflags &= ~IO_ISDIRECT;
848 xfs_iunlock(xip, iolock);
849 goto relock;
850 }
851 } else {
852 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
853 *offset, ioflags);
854 ret = generic_file_buffered_write(iocb, iovp, segs,
855 pos, offset, count, ret);
856 }
857
858 current->backing_dev_info = NULL;
859
860 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
861 ret = wait_on_sync_kiocb(iocb);
862
863 if ((ret == -ENOSPC) &&
864 DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
865 !(ioflags & IO_INVIS)) {
866
867 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100868 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800869 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
871 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
872 0, 0, 0); /* Delay flag intentionally unused */
Nathan Scotta13828b2006-03-17 17:26:14 +1100873 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800874 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 xfs_rwlock(bdp, locktype);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000876 if (error)
877 goto out_unlock_internal;
878 pos = xip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 ret = 0;
880 goto retry;
881 }
882
Yingping Lu68bdb6e2006-01-11 15:38:31 +1100883 isize = i_size_read(inode);
884 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
885 *offset = isize;
886
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000887 if (*offset > xip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 xfs_ilock(xip, XFS_ILOCK_EXCL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000889 if (*offset > xip->i_size)
890 xip->i_size = *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 xfs_iunlock(xip, XFS_ILOCK_EXCL);
892 }
893
894 error = -ret;
895 if (ret <= 0)
896 goto out_unlock_internal;
897
898 XFS_STATS_ADD(xs_write_bytes, ret);
899
900 /* Handle various SYNC-type writes */
901 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
Nathan Scott1e69dd02006-06-19 08:39:53 +1000902 error = xfs_write_sync_logforce(mp, xip);
903 if (error)
904 goto out_unlock_internal;
Nathan Scotta13828b2006-03-17 17:26:14 +1100905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100907 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800908 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 error = sync_page_range(inode, mapping, pos, ret);
911 if (!error)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000912 error = -ret;
913 if (need_i_mutex)
914 mutex_lock(&inode->i_mutex);
915 xfs_rwlock(bdp, locktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
918 out_unlock_internal:
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000919 if (io->io_new_size) {
920 xfs_ilock(xip, XFS_ILOCK_EXCL);
921 io->io_new_size = 0;
922 /*
923 * If this was a direct or synchronous I/O that failed (such
924 * as ENOSPC) then part of the I/O may have been written to
925 * disk before the error occured. In this case the on-disk
926 * file size may have been adjusted beyond the in-memory file
927 * size and now needs to be truncated back.
928 */
929 if (xip->i_d.di_size > xip->i_size)
930 xip->i_d.di_size = xip->i_size;
931 xfs_iunlock(xip, XFS_ILOCK_EXCL);
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 xfs_rwunlock(bdp, locktype);
Nathan Scotta13828b2006-03-17 17:26:14 +1100934 out_unlock_mutex:
935 if (need_i_mutex)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800936 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -error;
938}
939
940/*
941 * All xfs metadata buffers except log state machine buffers
942 * get this attached as their b_bdstrat callback function.
943 * This is so that we can catch a buffer
944 * after prematurely unpinning it to forcibly shutdown the filesystem.
945 */
946int
947xfs_bdstrat_cb(struct xfs_buf *bp)
948{
949 xfs_mount_t *mp;
950
951 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
952 if (!XFS_FORCED_SHUTDOWN(mp)) {
Nathan Scottce8e9222006-01-11 15:39:08 +1100953 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955 } else {
956 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
957 /*
958 * Metadata write that didn't get logged but
959 * written delayed anyway. These aren't associated
960 * with a transaction, and can be ignored.
961 */
962 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
963 (XFS_BUF_ISREAD(bp)) == 0)
964 return (xfs_bioerror_relse(bp));
965 else
966 return (xfs_bioerror(bp));
967 }
968}
969
970
971int
972xfs_bmap(bhv_desc_t *bdp,
973 xfs_off_t offset,
974 ssize_t count,
975 int flags,
976 xfs_iomap_t *iomapp,
977 int *niomaps)
978{
979 xfs_inode_t *ip = XFS_BHVTOI(bdp);
980 xfs_iocore_t *io = &ip->i_iocore;
981
982 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
983 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
984 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
985
986 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
987}
988
989/*
990 * Wrapper around bdstrat so that we can stop data
991 * from going to disk in case we are shutting down the filesystem.
992 * Typically user data goes thru this path; one of the exceptions
993 * is the superblock.
994 */
995int
996xfsbdstrat(
997 struct xfs_mount *mp,
998 struct xfs_buf *bp)
999{
1000 ASSERT(mp);
1001 if (!XFS_FORCED_SHUTDOWN(mp)) {
1002 /* Grio redirection would go here
1003 * if (XFS_BUF_IS_GRIO(bp)) {
1004 */
1005
Nathan Scottce8e9222006-01-11 15:39:08 +11001006 xfs_buf_iorequest(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return 0;
1008 }
1009
1010 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
1011 return (xfs_bioerror_relse(bp));
1012}
1013
1014/*
1015 * If the underlying (data/log/rt) device is readonly, there are some
1016 * operations that cannot proceed.
1017 */
1018int
1019xfs_dev_is_read_only(
1020 xfs_mount_t *mp,
1021 char *message)
1022{
1023 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1024 xfs_readonly_buftarg(mp->m_logdev_targp) ||
1025 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1026 cmn_err(CE_NOTE,
1027 "XFS: %s required on read-only device.", message);
1028 cmn_err(CE_NOTE,
1029 "XFS: write access unavailable, cannot proceed.");
1030 return EROFS;
1031 }
1032 return 0;
1033}