[XFS] Fix race condition in xfs_write().
[deliverable/linux.git] / fs / xfs / linux-2.6 / xfs_lrw.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
1da177e4 19#include "xfs_fs.h"
a844f451 20#include "xfs_bit.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
1da177e4
LT
26#include "xfs_dir2.h"
27#include "xfs_alloc.h"
28#include "xfs_dmapi.h"
29#include "xfs_quota.h"
30#include "xfs_mount.h"
1da177e4 31#include "xfs_bmap_btree.h"
a844f451 32#include "xfs_alloc_btree.h"
1da177e4 33#include "xfs_ialloc_btree.h"
1da177e4 34#include "xfs_dir2_sf.h"
a844f451 35#include "xfs_attr_sf.h"
1da177e4
LT
36#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_bmap.h"
a844f451
NS
39#include "xfs_btree.h"
40#include "xfs_ialloc.h"
1da177e4
LT
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
1da177e4
LT
46#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)),
f1fdc848 82 (void *)((unsigned long)current_pid()),
1da177e4
LT
83 (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)),
f1fdc848 112 (void *)((unsigned long)current_pid()),
1da177e4
LT
113 (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 */
68160161 135 size_t count) /* size of data to zero */
1da177e4
LT
136{
137 unsigned bytes;
138 struct page *page;
139 struct address_space *mapping;
1da177e4
LT
140 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
1da177e4
LT
157 status = mapping->a_ops->prepare_write(NULL, page, offset,
158 offset + bytes);
e7ff6aed 159 if (status)
1da177e4 160 goto unlock;
1da177e4 161
e7ff6aed
DC
162 memclear_highpage_flush(page, offset, bytes);
163
1da177e4
LT
164 status = mapping->a_ops->commit_write(NULL, page, offset,
165 offset + bytes);
166 if (!status) {
167 pos += bytes;
168 count -= bytes;
1da177e4
LT
169 }
170
171unlock:
1da177e4
LT
172 unlock_page(page);
173 page_cache_release(page);
174 if (status)
175 break;
176 } while (count);
177
178 return (-status);
179}
180
1da177e4
LT
181ssize_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;
d3cf2094 194 ssize_t ret = 0;
1da177e4
LT
195 xfs_fsize_t n;
196 xfs_inode_t *ip;
197 xfs_mount_t *mp;
67fcaa73 198 bhv_vnode_t *vp;
1da177e4
LT
199 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;
ce8e922c
NS
225 if ((*offset & target->bt_smask) ||
226 (size & target->bt_smask)) {
1da177e4
LT
227 if (*offset == ip->i_d.di_size) {
228 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
a13828b1 241 if (XFS_FORCED_SHUTDOWN(mp))
1da177e4 242 return -EIO;
1da177e4
LT
243
244 if (unlikely(ioflags & IO_ISDIRECT))
1b1dcc1b 245 mutex_lock(&inode->i_mutex);
1da177e4
LT
246 xfs_ilock(ip, XFS_IOLOCK_SHARED);
247
248 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
249 !(ioflags & IO_INVIS)) {
8285fb58 250 bhv_vrwlock_t locktype = VRWLOCK_READ;
e1a40fa9 251 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
1da177e4
LT
252
253 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ,
254 BHV_TO_VNODE(bdp), *offset, size,
e1a40fa9 255 dmflags, &locktype);
1da177e4
LT
256 if (ret) {
257 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
721259bc
LM
258 if (unlikely(ioflags & IO_ISDIRECT))
259 mutex_unlock(&inode->i_mutex);
260 return ret;
1da177e4
LT
261 }
262 }
263
edcd4bce
NS
264 if (unlikely(ioflags & IO_ISDIRECT)) {
265 if (VN_CACHED(vp))
d3cf2094 266 ret = bhv_vop_flushinval_pages(vp, ctooff(offtoct(*offset)),
edcd4bce 267 -1, FI_REMAPF_LOCKED);
721259bc 268 mutex_unlock(&inode->i_mutex);
d3cf2094
LM
269 if (ret) {
270 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
271 return ret;
272 }
edcd4bce 273 }
721259bc 274
1da177e4
LT
275 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
276 (void *)iovp, segs, *offset, ioflags);
543ade1f
BP
277
278 iocb->ki_pos = *offset;
279 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
1da177e4
LT
280 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);
1da177e4
LT
286 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{
1b895840
NS
300 xfs_inode_t *ip = XFS_BHVTOI(bdp);
301 xfs_mount_t *mp = ip->i_mount;
1da177e4 302 ssize_t ret;
1da177e4
LT
303
304 XFS_STATS_INC(xs_read_calls);
1b895840 305 if (XFS_FORCED_SHUTDOWN(mp))
1da177e4
LT
306 return -EIO;
307
308 xfs_ilock(ip, XFS_IOLOCK_SHARED);
309
1b895840 310 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_READ) &&
1da177e4 311 (!(ioflags & IO_INVIS))) {
8285fb58 312 bhv_vrwlock_t locktype = VRWLOCK_READ;
1da177e4
LT
313 int error;
314
1b895840
NS
315 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
316 *offset, count,
1da177e4
LT
317 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);
1b895840
NS
326 if (ret > 0)
327 XFS_STATS_ADD(xs_read_bytes, ret);
1da177e4
LT
328
329 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
1b895840
NS
330 return ret;
331}
332
333ssize_t
334xfs_splice_read(
335 bhv_desc_t *bdp,
336 struct file *infilp,
cbb7e577 337 loff_t *ppos,
3a326a2c 338 struct pipe_inode_info *pipe,
1b895840
NS
339 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;
1da177e4 347
1b895840
NS
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))) {
8285fb58 356 bhv_vrwlock_t locktype = VRWLOCK_READ;
1b895840
NS
357 int error;
358
359 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp),
cbb7e577 360 *ppos, count,
1b895840
NS
361 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,
cbb7e577
JA
368 pipe, count, *ppos, ioflags);
369 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
1da177e4
LT
370 if (ret > 0)
371 XFS_STATS_ADD(xs_read_bytes, ret);
372
1b895840
NS
373 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
374 return ret;
375}
376
377ssize_t
378xfs_splice_write(
379 bhv_desc_t *bdp,
3a326a2c 380 struct pipe_inode_info *pipe,
1b895840 381 struct file *outfilp,
cbb7e577 382 loff_t *ppos,
1b895840
NS
383 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;
390 ssize_t ret;
0a8d17d0
DC
391 struct inode *inode = outfilp->f_mapping->host;
392 xfs_fsize_t isize;
1b895840
NS
393
394 XFS_STATS_INC(xs_write_calls);
395 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
396 return -EIO;
397
398 xfs_ilock(ip, XFS_IOLOCK_EXCL);
399
400 if (DM_EVENT_ENABLED(BHV_TO_VNODE(bdp)->v_vfsp, ip, DM_EVENT_WRITE) &&
401 (!(ioflags & IO_INVIS))) {
8285fb58 402 bhv_vrwlock_t locktype = VRWLOCK_WRITE;
1b895840
NS
403 int error;
404
405 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, BHV_TO_VNODE(bdp),
cbb7e577 406 *ppos, count,
1b895840
NS
407 FILP_DELAY_FLAG(outfilp), &locktype);
408 if (error) {
409 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
410 return -error;
411 }
412 }
413 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore,
cbb7e577
JA
414 pipe, count, *ppos, ioflags);
415 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
1b895840
NS
416 if (ret > 0)
417 XFS_STATS_ADD(xs_write_bytes, ret);
418
0a8d17d0
DC
419 isize = i_size_read(inode);
420 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
421 *ppos = isize;
422
423 if (*ppos > ip->i_d.di_size) {
424 xfs_ilock(ip, XFS_ILOCK_EXCL);
425 if (*ppos > ip->i_d.di_size) {
426 ip->i_d.di_size = *ppos;
427 i_size_write(inode, *ppos);
428 ip->i_update_core = 1;
429 ip->i_update_size = 1;
430 }
431 xfs_iunlock(ip, XFS_ILOCK_EXCL);
432 }
1b895840 433 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1da177e4
LT
434 return ret;
435}
436
437/*
438 * This routine is called to handle zeroing any space in the last
439 * block of the file that is beyond the EOF. We do this since the
440 * size is being increased without writing anything to that block
441 * and we don't want anyone to read the garbage on the disk.
442 */
443STATIC int /* error (positive) */
444xfs_zero_last_block(
445 struct inode *ip,
446 xfs_iocore_t *io,
68160161
LM
447 xfs_fsize_t offset,
448 xfs_fsize_t isize)
1da177e4
LT
449{
450 xfs_fileoff_t last_fsb;
1b895840 451 xfs_mount_t *mp = io->io_mount;
1da177e4
LT
452 int nimaps;
453 int zero_offset;
454 int zero_len;
1da177e4
LT
455 int error = 0;
456 xfs_bmbt_irec_t imap;
1da177e4
LT
457
458 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
1da177e4 459
24ee8088
ES
460 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
461 if (zero_offset == 0) {
1da177e4
LT
462 /*
463 * There are no extra bytes in the last block on disk to
464 * zero, so return.
465 */
466 return 0;
467 }
468
469 last_fsb = XFS_B_TO_FSBT(mp, isize);
470 nimaps = 1;
471 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
3e57ecf6 472 &nimaps, NULL, NULL);
1da177e4
LT
473 if (error) {
474 return error;
475 }
476 ASSERT(nimaps > 0);
477 /*
478 * If the block underlying isize is just a hole, then there
479 * is nothing to zero.
480 */
481 if (imap.br_startblock == HOLESTARTBLOCK) {
482 return 0;
483 }
484 /*
485 * Zero the part of the last block beyond the EOF, and write it
486 * out sync. We need to drop the ilock while we do this so we
487 * don't deadlock when the buffer cache calls back to us.
488 */
489 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
1da177e4 490
24ee8088 491 zero_len = mp->m_sb.sb_blocksize - zero_offset;
68160161
LM
492 if (isize + zero_len > offset)
493 zero_len = offset - isize;
494 error = xfs_iozero(ip, isize, zero_len);
1da177e4
LT
495
496 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
497 ASSERT(error >= 0);
498 return error;
499}
500
501/*
502 * Zero any on disk space between the current EOF and the new,
503 * larger EOF. This handles the normal case of zeroing the remainder
504 * of the last block in the file and the unusual case of zeroing blocks
505 * out beyond the size of the file. This second case only happens
506 * with fixed size extents and when the system crashes before the inode
507 * size was updated but after blocks were allocated. If fill is set,
508 * then any holes in the range are filled and zeroed. If not, the holes
509 * are left alone as holes.
510 */
511
512int /* error (positive) */
513xfs_zero_eof(
67fcaa73 514 bhv_vnode_t *vp,
1da177e4
LT
515 xfs_iocore_t *io,
516 xfs_off_t offset, /* starting I/O offset */
68160161 517 xfs_fsize_t isize) /* current inode size */
1da177e4 518{
ec86dc02 519 struct inode *ip = vn_to_inode(vp);
1da177e4
LT
520 xfs_fileoff_t start_zero_fsb;
521 xfs_fileoff_t end_zero_fsb;
1da177e4
LT
522 xfs_fileoff_t zero_count_fsb;
523 xfs_fileoff_t last_fsb;
68160161
LM
524 xfs_fileoff_t zero_off;
525 xfs_fsize_t zero_len;
1b895840 526 xfs_mount_t *mp = io->io_mount;
1da177e4
LT
527 int nimaps;
528 int error = 0;
529 xfs_bmbt_irec_t imap;
1da177e4
LT
530
531 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
532 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
24ee8088 533 ASSERT(offset > isize);
1da177e4 534
1da177e4
LT
535 /*
536 * First handle zeroing the block on which isize resides.
537 * We only zero a part of that block so it is handled specially.
538 */
68160161 539 error = xfs_zero_last_block(ip, io, offset, isize);
1da177e4
LT
540 if (error) {
541 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
542 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
543 return error;
544 }
545
546 /*
547 * Calculate the range between the new size and the old
548 * where blocks needing to be zeroed may exist. To get the
549 * block where the last byte in the file currently resides,
550 * we need to subtract one from the size and truncate back
551 * to a block boundary. We subtract 1 in case the size is
552 * exactly on a block boundary.
553 */
554 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
555 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
556 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
557 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
558 if (last_fsb == end_zero_fsb) {
559 /*
560 * The size was only incremented on its last block.
561 * We took care of that above, so just return.
562 */
563 return 0;
564 }
565
566 ASSERT(start_zero_fsb <= end_zero_fsb);
1da177e4
LT
567 while (start_zero_fsb <= end_zero_fsb) {
568 nimaps = 1;
569 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
570 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
3e57ecf6 571 0, NULL, 0, &imap, &nimaps, NULL, NULL);
1da177e4
LT
572 if (error) {
573 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
574 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
575 return error;
576 }
577 ASSERT(nimaps > 0);
578
579 if (imap.br_state == XFS_EXT_UNWRITTEN ||
580 imap.br_startblock == HOLESTARTBLOCK) {
581 /*
582 * This loop handles initializing pages that were
583 * partially initialized by the code below this
584 * loop. It basically zeroes the part of the page
585 * that sits on a hole and sets the page as P_HOLE
586 * and calls remapf if it is a mapped file.
587 */
24ee8088 588 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
1da177e4
LT
589 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
590 continue;
591 }
592
593 /*
3d80ede4 594 * There are blocks we need to zero.
1da177e4
LT
595 * Drop the inode lock while we're doing the I/O.
596 * We'll still have the iolock to protect us.
597 */
598 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
599
68160161
LM
600 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
601 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
602
603 if ((zero_off + zero_len) > offset)
604 zero_len = offset - zero_off;
605
606 error = xfs_iozero(ip, zero_off, zero_len);
1da177e4
LT
607 if (error) {
608 goto out_lock;
609 }
610
3d80ede4 611 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
1da177e4
LT
612 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
613
614 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
615 }
616
617 return 0;
618
619out_lock:
620
621 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
622 ASSERT(error >= 0);
623 return error;
624}
625
626ssize_t /* bytes written, or (-) error */
627xfs_write(
628 bhv_desc_t *bdp,
629 struct kiocb *iocb,
630 const struct iovec *iovp,
631 unsigned int nsegs,
632 loff_t *offset,
633 int ioflags,
634 cred_t *credp)
635{
636 struct file *file = iocb->ki_filp;
637 struct address_space *mapping = file->f_mapping;
638 struct inode *inode = mapping->host;
639 unsigned long segs = nsegs;
640 xfs_inode_t *xip;
641 xfs_mount_t *mp;
642 ssize_t ret = 0, error = 0;
643 xfs_fsize_t isize, new_size;
644 xfs_iocore_t *io;
67fcaa73 645 bhv_vnode_t *vp;
1da177e4
LT
646 unsigned long seg;
647 int iolock;
648 int eventsent = 0;
8285fb58 649 bhv_vrwlock_t locktype;
1da177e4
LT
650 size_t ocount = 0, count;
651 loff_t pos;
2a329631 652 int need_i_mutex;
1da177e4
LT
653
654 XFS_STATS_INC(xs_write_calls);
655
656 vp = BHV_TO_VNODE(bdp);
657 xip = XFS_BHVTOI(bdp);
658
659 for (seg = 0; seg < segs; seg++) {
660 const struct iovec *iv = &iovp[seg];
661
662 /*
663 * If any segment has a negative length, or the cumulative
664 * length ever wraps negative then return -EINVAL.
665 */
666 ocount += iv->iov_len;
667 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
668 return -EINVAL;
669 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
670 continue;
671 if (seg == 0)
672 return -EFAULT;
673 segs = seg;
674 ocount -= iv->iov_len; /* This segment is no good */
675 break;
676 }
677
678 count = ocount;
679 pos = *offset;
680
681 if (count == 0)
682 return 0;
683
684 io = &xip->i_iocore;
685 mp = io->io_mount;
686
34327e13
NS
687 vfs_wait_for_freeze(vp->v_vfsp, SB_FREEZE_WRITE);
688
1da177e4
LT
689 if (XFS_FORCED_SHUTDOWN(mp))
690 return -EIO;
691
1da177e4 692relock:
2a329631
LM
693 if (ioflags & IO_ISDIRECT) {
694 iolock = XFS_IOLOCK_SHARED;
695 locktype = VRWLOCK_WRITE_DIRECT;
696 need_i_mutex = 0;
697 } else {
1da177e4
LT
698 iolock = XFS_IOLOCK_EXCL;
699 locktype = VRWLOCK_WRITE;
2a329631 700 need_i_mutex = 1;
1b1dcc1b 701 mutex_lock(&inode->i_mutex);
1da177e4
LT
702 }
703
704 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
705
1da177e4
LT
706start:
707 error = -generic_write_checks(file, &pos, &count,
708 S_ISBLK(inode->i_mode));
709 if (error) {
710 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
a13828b1 711 goto out_unlock_mutex;
1da177e4
LT
712 }
713
2a329631
LM
714 isize = i_size_read(inode);
715
716 if (ioflags & IO_ISDIRECT) {
717 xfs_buftarg_t *target =
718 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
719 mp->m_rtdev_targp : mp->m_ddev_targp;
720
721 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
722 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
723 return XFS_ERROR(-EINVAL);
724 }
725
726 if (!need_i_mutex && (VN_CACHED(vp) || pos > isize)) {
727 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
728 iolock = XFS_IOLOCK_EXCL;
729 locktype = VRWLOCK_WRITE;
730 need_i_mutex = 1;
731 mutex_lock(&inode->i_mutex);
732 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
733 goto start;
734 }
735 }
736
1da177e4
LT
737 new_size = pos + count;
738 if (new_size > isize)
739 io->io_new_size = new_size;
740
741 if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
742 !(ioflags & IO_INVIS) && !eventsent)) {
743 loff_t savedsize = pos;
744 int dmflags = FILP_DELAY_FLAG(file);
745
a13828b1 746 if (need_i_mutex)
1b1dcc1b 747 dmflags |= DM_FLAGS_IMUX;
1da177e4
LT
748
749 xfs_iunlock(xip, XFS_ILOCK_EXCL);
750 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
751 pos, count,
752 dmflags, &locktype);
753 if (error) {
754 xfs_iunlock(xip, iolock);
a13828b1 755 goto out_unlock_mutex;
1da177e4
LT
756 }
757 xfs_ilock(xip, XFS_ILOCK_EXCL);
758 eventsent = 1;
759
760 /*
c41564b5 761 * The iolock was dropped and reacquired in XFS_SEND_DATA
1da177e4
LT
762 * so we have to recheck the size when appending.
763 * We will only "goto start;" once, since having sent the
764 * event prevents another call to XFS_SEND_DATA, which is
765 * what allows the size to change in the first place.
766 */
767 if ((file->f_flags & O_APPEND) && savedsize != isize) {
1da177e4
LT
768 goto start;
769 }
770 }
771
4aeb664c 772 if (likely(!(ioflags & IO_INVIS))) {
870f4817 773 file_update_time(file);
4aeb664c
NS
774 xfs_ichgtime_fast(xip, inode,
775 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1da177e4
LT
776 }
777
778 /*
779 * If the offset is beyond the size of the file, we have a couple
780 * of things to do. First, if there is already space allocated
781 * we need to either create holes or zero the disk or ...
782 *
783 * If there is a page where the previous size lands, we need
784 * to zero it out up to the new size.
785 */
786
787 if (pos > isize) {
68160161 788 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos, isize);
1da177e4
LT
789 if (error) {
790 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
a13828b1 791 goto out_unlock_mutex;
1da177e4
LT
792 }
793 }
794 xfs_iunlock(xip, XFS_ILOCK_EXCL);
795
796 /*
797 * If we're writing the file then make sure to clear the
798 * setuid and setgid bits if the process is not being run
799 * by root. This keeps people from modifying setuid and
800 * setgid binaries.
801 */
802
803 if (((xip->i_d.di_mode & S_ISUID) ||
804 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
805 (S_ISGID | S_IXGRP))) &&
806 !capable(CAP_FSETID)) {
807 error = xfs_write_clear_setuid(xip);
808 if (likely(!error))
e678fb0d 809 error = -remove_suid(file->f_path.dentry);
1da177e4
LT
810 if (unlikely(error)) {
811 xfs_iunlock(xip, iolock);
a13828b1 812 goto out_unlock_mutex;
1da177e4
LT
813 }
814 }
815
816retry:
817 /* We can write back this queue in page reclaim */
818 current->backing_dev_info = mapping->backing_dev_info;
819
820 if ((ioflags & IO_ISDIRECT)) {
2a329631
LM
821 if (VN_CACHED(vp)) {
822 WARN_ON(need_i_mutex == 0);
1da177e4
LT
823 xfs_inval_cached_trace(io, pos, -1,
824 ctooff(offtoct(pos)), -1);
d3cf2094 825 error = bhv_vop_flushinval_pages(vp, ctooff(offtoct(pos)),
1da177e4 826 -1, FI_REMAPF_LOCKED);
d3cf2094
LM
827 if (error)
828 goto out_unlock_internal;
1da177e4
LT
829 }
830
a13828b1 831 if (need_i_mutex) {
1da177e4
LT
832 /* demote the lock now the cached pages are gone */
833 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
1b1dcc1b 834 mutex_unlock(&inode->i_mutex);
1da177e4
LT
835
836 iolock = XFS_IOLOCK_SHARED;
837 locktype = VRWLOCK_WRITE_DIRECT;
a13828b1 838 need_i_mutex = 0;
1da177e4
LT
839 }
840
841 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
842 *offset, ioflags);
843 ret = generic_file_direct_write(iocb, iovp,
844 &segs, pos, offset, count, ocount);
845
846 /*
847 * direct-io write to a hole: fall through to buffered I/O
848 * for completing the rest of the request.
849 */
850 if (ret >= 0 && ret != count) {
851 XFS_STATS_ADD(xs_write_bytes, ret);
852
853 pos += ret;
854 count -= ret;
855
1da177e4
LT
856 ioflags &= ~IO_ISDIRECT;
857 xfs_iunlock(xip, iolock);
858 goto relock;
859 }
860 } else {
861 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
862 *offset, ioflags);
863 ret = generic_file_buffered_write(iocb, iovp, segs,
864 pos, offset, count, ret);
865 }
866
867 current->backing_dev_info = NULL;
868
869 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
870 ret = wait_on_sync_kiocb(iocb);
871
872 if ((ret == -ENOSPC) &&
873 DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
874 !(ioflags & IO_INVIS)) {
875
876 xfs_rwunlock(bdp, locktype);
a13828b1 877 if (need_i_mutex)
1b1dcc1b 878 mutex_unlock(&inode->i_mutex);
1da177e4
LT
879 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
880 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
881 0, 0, 0); /* Delay flag intentionally unused */
882 if (error)
e1a40fa9 883 goto out_nounlocks;
a13828b1 884 if (need_i_mutex)
1b1dcc1b 885 mutex_lock(&inode->i_mutex);
1da177e4
LT
886 xfs_rwlock(bdp, locktype);
887 pos = xip->i_d.di_size;
888 ret = 0;
889 goto retry;
890 }
891
68bdb6ea
YL
892 isize = i_size_read(inode);
893 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
894 *offset = isize;
895
1da177e4
LT
896 if (*offset > xip->i_d.di_size) {
897 xfs_ilock(xip, XFS_ILOCK_EXCL);
898 if (*offset > xip->i_d.di_size) {
899 xip->i_d.di_size = *offset;
900 i_size_write(inode, *offset);
901 xip->i_update_core = 1;
902 xip->i_update_size = 1;
903 }
904 xfs_iunlock(xip, XFS_ILOCK_EXCL);
905 }
906
907 error = -ret;
908 if (ret <= 0)
909 goto out_unlock_internal;
910
911 XFS_STATS_ADD(xs_write_bytes, ret);
912
913 /* Handle various SYNC-type writes */
914 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
1e69dd0e
NS
915 error = xfs_write_sync_logforce(mp, xip);
916 if (error)
917 goto out_unlock_internal;
a13828b1 918
1da177e4 919 xfs_rwunlock(bdp, locktype);
a13828b1 920 if (need_i_mutex)
1b1dcc1b 921 mutex_unlock(&inode->i_mutex);
1da177e4
LT
922
923 error = sync_page_range(inode, mapping, pos, ret);
924 if (!error)
925 error = ret;
926 return error;
927 }
928
929 out_unlock_internal:
930 xfs_rwunlock(bdp, locktype);
a13828b1
NS
931 out_unlock_mutex:
932 if (need_i_mutex)
1b1dcc1b 933 mutex_unlock(&inode->i_mutex);
e1a40fa9 934 out_nounlocks:
1da177e4
LT
935 return -error;
936}
937
938/*
939 * All xfs metadata buffers except log state machine buffers
940 * get this attached as their b_bdstrat callback function.
941 * This is so that we can catch a buffer
942 * after prematurely unpinning it to forcibly shutdown the filesystem.
943 */
944int
945xfs_bdstrat_cb(struct xfs_buf *bp)
946{
947 xfs_mount_t *mp;
948
949 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
950 if (!XFS_FORCED_SHUTDOWN(mp)) {
ce8e922c 951 xfs_buf_iorequest(bp);
1da177e4
LT
952 return 0;
953 } else {
954 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
955 /*
956 * Metadata write that didn't get logged but
957 * written delayed anyway. These aren't associated
958 * with a transaction, and can be ignored.
959 */
960 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
961 (XFS_BUF_ISREAD(bp)) == 0)
962 return (xfs_bioerror_relse(bp));
963 else
964 return (xfs_bioerror(bp));
965 }
966}
967
968
969int
970xfs_bmap(bhv_desc_t *bdp,
971 xfs_off_t offset,
972 ssize_t count,
973 int flags,
974 xfs_iomap_t *iomapp,
975 int *niomaps)
976{
977 xfs_inode_t *ip = XFS_BHVTOI(bdp);
978 xfs_iocore_t *io = &ip->i_iocore;
979
980 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
981 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
982 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
983
984 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
985}
986
987/*
988 * Wrapper around bdstrat so that we can stop data
989 * from going to disk in case we are shutting down the filesystem.
990 * Typically user data goes thru this path; one of the exceptions
991 * is the superblock.
992 */
993int
994xfsbdstrat(
995 struct xfs_mount *mp,
996 struct xfs_buf *bp)
997{
998 ASSERT(mp);
999 if (!XFS_FORCED_SHUTDOWN(mp)) {
1000 /* Grio redirection would go here
1001 * if (XFS_BUF_IS_GRIO(bp)) {
1002 */
1003
ce8e922c 1004 xfs_buf_iorequest(bp);
1da177e4
LT
1005 return 0;
1006 }
1007
1008 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
1009 return (xfs_bioerror_relse(bp));
1010}
1011
1012/*
1013 * If the underlying (data/log/rt) device is readonly, there are some
1014 * operations that cannot proceed.
1015 */
1016int
1017xfs_dev_is_read_only(
1018 xfs_mount_t *mp,
1019 char *message)
1020{
1021 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1022 xfs_readonly_buftarg(mp->m_logdev_targp) ||
1023 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1024 cmn_err(CE_NOTE,
1025 "XFS: %s required on read-only device.", message);
1026 cmn_err(CE_NOTE,
1027 "XFS: write access unavailable, cannot proceed.");
1028 return EROFS;
1029 }
1030 return 0;
1031}
This page took 0.258709 seconds and 5 git commands to generate.