xfs: event tracing support
[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"
1da177e4
LT
45#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"
993386c1 50#include "xfs_vnodeops.h"
0b1b213f 51#include "xfs_trace.h"
1da177e4
LT
52
53#include <linux/capability.h>
54#include <linux/writeback.h>
55
56
1da177e4
LT
57/*
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(
541d7d3c 69 struct xfs_inode *ip, /* inode */
1da177e4 70 loff_t pos, /* offset in file */
68160161 71 size_t count) /* size of data to zero */
1da177e4 72{
1da177e4
LT
73 struct page *page;
74 struct address_space *mapping;
1da177e4
LT
75 int status;
76
01651646 77 mapping = VFS_I(ip)->i_mapping;
1da177e4 78 do {
d79689c7
NP
79 unsigned offset, bytes;
80 void *fsdata;
1da177e4
LT
81
82 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1da177e4
LT
83 bytes = PAGE_CACHE_SIZE - offset;
84 if (bytes > count)
85 bytes = count;
86
d79689c7
NP
87 status = pagecache_write_begin(NULL, mapping, pos, bytes,
88 AOP_FLAG_UNINTERRUPTIBLE,
89 &page, &fsdata);
e7ff6aed 90 if (status)
d79689c7 91 break;
1da177e4 92
eebd2aa3 93 zero_user(page, offset, bytes);
e7ff6aed 94
d79689c7
NP
95 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;
1da177e4
LT
101 } while (count);
102
103 return (-status);
104}
105
1da177e4
LT
106ssize_t /* bytes read, or (-) error */
107xfs_read(
993386c1 108 xfs_inode_t *ip,
1da177e4
LT
109 struct kiocb *iocb,
110 const struct iovec *iovp,
111 unsigned int segs,
112 loff_t *offset,
993386c1 113 int ioflags)
1da177e4
LT
114{
115 struct file *file = iocb->ki_filp;
116 struct inode *inode = file->f_mapping->host;
993386c1 117 xfs_mount_t *mp = ip->i_mount;
1da177e4 118 size_t size = 0;
d3cf2094 119 ssize_t ret = 0;
1da177e4 120 xfs_fsize_t n;
1da177e4
LT
121 unsigned long seg;
122
1da177e4
LT
123
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 =
71ddabb9 142 XFS_IS_REALTIME_INODE(ip) ?
1da177e4 143 mp->m_rtdev_targp : mp->m_ddev_targp;
ce8e922c
NS
144 if ((*offset & target->bt_smask) ||
145 (size & target->bt_smask)) {
ba87ea69 146 if (*offset == ip->i_size) {
1da177e4
LT
147 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
a13828b1 160 if (XFS_FORCED_SHUTDOWN(mp))
1da177e4 161 return -EIO;
1da177e4
LT
162
163 if (unlikely(ioflags & IO_ISDIRECT))
1b1dcc1b 164 mutex_lock(&inode->i_mutex);
1da177e4
LT
165 xfs_ilock(ip, XFS_IOLOCK_SHARED);
166
eb9df39d 167 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
e1a40fa9 168 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
126468b1 169 int iolock = XFS_IOLOCK_SHARED;
1da177e4 170
bc4ac74a 171 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *offset, size,
126468b1 172 dmflags, &iolock);
1da177e4
LT
173 if (ret) {
174 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
721259bc
LM
175 if (unlikely(ioflags & IO_ISDIRECT))
176 mutex_unlock(&inode->i_mutex);
177 return ret;
1da177e4
LT
178 }
179 }
180
edcd4bce 181 if (unlikely(ioflags & IO_ISDIRECT)) {
dcf49cc5 182 if (inode->i_mapping->nrpages)
2e656092 183 ret = -xfs_flushinval_pages(ip, (*offset & PAGE_CACHE_MASK),
e6a4b37f 184 -1, FI_REMAPF_LOCKED);
721259bc 185 mutex_unlock(&inode->i_mutex);
d3cf2094
LM
186 if (ret) {
187 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
188 return ret;
189 }
edcd4bce 190 }
721259bc 191
0b1b213f 192 trace_xfs_file_read(ip, size, *offset, ioflags);
543ade1f
BP
193
194 iocb->ki_pos = *offset;
195 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
1da177e4
LT
196 if (ret > 0)
197 XFS_STATS_ADD(xs_read_bytes, ret);
198
199 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
1da177e4
LT
200 return ret;
201}
202
1b895840
NS
203ssize_t
204xfs_splice_read(
993386c1 205 xfs_inode_t *ip,
1b895840 206 struct file *infilp,
cbb7e577 207 loff_t *ppos,
3a326a2c 208 struct pipe_inode_info *pipe,
1b895840
NS
209 size_t count,
210 int flags,
993386c1 211 int ioflags)
1b895840 212{
1b895840
NS
213 xfs_mount_t *mp = ip->i_mount;
214 ssize_t ret;
1da177e4 215
1b895840
NS
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
eb9df39d 222 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
126468b1 223 int iolock = XFS_IOLOCK_SHARED;
1b895840
NS
224 int error;
225
bc4ac74a 226 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *ppos, count,
126468b1 227 FILP_DELAY_FLAG(infilp), &iolock);
1b895840
NS
228 if (error) {
229 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
230 return -error;
231 }
232 }
0b1b213f
CH
233
234 trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
235
cbb7e577 236 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
1da177e4
LT
237 if (ret > 0)
238 XFS_STATS_ADD(xs_read_bytes, ret);
239
1b895840
NS
240 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
241 return ret;
242}
243
244ssize_t
245xfs_splice_write(
993386c1 246 xfs_inode_t *ip,
3a326a2c 247 struct pipe_inode_info *pipe,
1b895840 248 struct file *outfilp,
cbb7e577 249 loff_t *ppos,
1b895840
NS
250 size_t count,
251 int flags,
993386c1 252 int ioflags)
1b895840 253{
1b895840
NS
254 xfs_mount_t *mp = ip->i_mount;
255 ssize_t ret;
0a8d17d0 256 struct inode *inode = outfilp->f_mapping->host;
ba87ea69 257 xfs_fsize_t isize, new_size;
1b895840
NS
258
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
eb9df39d 265 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
126468b1 266 int iolock = XFS_IOLOCK_EXCL;
1b895840
NS
267 int error;
268
bc4ac74a 269 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, *ppos, count,
126468b1 270 FILP_DELAY_FLAG(outfilp), &iolock);
1b895840
NS
271 if (error) {
272 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
273 return -error;
274 }
275 }
ba87ea69
LM
276
277 new_size = *ppos + count;
278
279 xfs_ilock(ip, XFS_ILOCK_EXCL);
280 if (new_size > ip->i_size)
613d7043 281 ip->i_new_size = new_size;
ba87ea69
LM
282 xfs_iunlock(ip, XFS_ILOCK_EXCL);
283
0b1b213f
CH
284 trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
285
cbb7e577 286 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
1b895840
NS
287 if (ret > 0)
288 XFS_STATS_ADD(xs_write_bytes, ret);
289
0a8d17d0
DC
290 isize = i_size_read(inode);
291 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
292 *ppos = isize;
293
ba87ea69 294 if (*ppos > ip->i_size) {
0a8d17d0 295 xfs_ilock(ip, XFS_ILOCK_EXCL);
ba87ea69
LM
296 if (*ppos > ip->i_size)
297 ip->i_size = *ppos;
298 xfs_iunlock(ip, XFS_ILOCK_EXCL);
299 }
300
613d7043 301 if (ip->i_new_size) {
ba87ea69 302 xfs_ilock(ip, XFS_ILOCK_EXCL);
613d7043 303 ip->i_new_size = 0;
ba87ea69
LM
304 if (ip->i_d.di_size > ip->i_size)
305 ip->i_d.di_size = ip->i_size;
0a8d17d0
DC
306 xfs_iunlock(ip, XFS_ILOCK_EXCL);
307 }
1b895840 308 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1da177e4
LT
309 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(
541d7d3c 320 xfs_inode_t *ip,
68160161
LM
321 xfs_fsize_t offset,
322 xfs_fsize_t isize)
1da177e4
LT
323{
324 xfs_fileoff_t last_fsb;
541d7d3c 325 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
326 int nimaps;
327 int zero_offset;
328 int zero_len;
1da177e4
LT
329 int error = 0;
330 xfs_bmbt_irec_t imap;
1da177e4 331
579aa9ca 332 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1da177e4 333
24ee8088
ES
334 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
335 if (zero_offset == 0) {
1da177e4
LT
336 /*
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;
541d7d3c 345 error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
3e57ecf6 346 &nimaps, NULL, NULL);
1da177e4
LT
347 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 */
579aa9ca 363 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1da177e4 364
24ee8088 365 zero_len = mp->m_sb.sb_blocksize - zero_offset;
68160161
LM
366 if (isize + zero_len > offset)
367 zero_len = offset - isize;
368 error = xfs_iozero(ip, isize, zero_len);
1da177e4 369
579aa9ca 370 xfs_ilock(ip, XFS_ILOCK_EXCL);
1da177e4
LT
371 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(
541d7d3c 388 xfs_inode_t *ip,
1da177e4 389 xfs_off_t offset, /* starting I/O offset */
68160161 390 xfs_fsize_t isize) /* current inode size */
1da177e4 391{
613d7043 392 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
393 xfs_fileoff_t start_zero_fsb;
394 xfs_fileoff_t end_zero_fsb;
1da177e4
LT
395 xfs_fileoff_t zero_count_fsb;
396 xfs_fileoff_t last_fsb;
68160161
LM
397 xfs_fileoff_t zero_off;
398 xfs_fsize_t zero_len;
1da177e4
LT
399 int nimaps;
400 int error = 0;
401 xfs_bmbt_irec_t imap;
1da177e4 402
579aa9ca 403 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
24ee8088 404 ASSERT(offset > isize);
1da177e4 405
1da177e4
LT
406 /*
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 */
541d7d3c 410 error = xfs_zero_last_block(ip, offset, isize);
1da177e4 411 if (error) {
579aa9ca 412 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
1da177e4
LT
413 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);
1da177e4
LT
437 while (start_zero_fsb <= end_zero_fsb) {
438 nimaps = 1;
439 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
541d7d3c 440 error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
3e57ecf6 441 0, NULL, 0, &imap, &nimaps, NULL, NULL);
1da177e4 442 if (error) {
579aa9ca 443 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
1da177e4
LT
444 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 */
24ee8088 457 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
1da177e4
LT
458 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
459 continue;
460 }
461
462 /*
3d80ede4 463 * There are blocks we need to zero.
1da177e4
LT
464 * Drop the inode lock while we're doing the I/O.
465 * We'll still have the iolock to protect us.
466 */
579aa9ca 467 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1da177e4 468
68160161
LM
469 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);
1da177e4
LT
476 if (error) {
477 goto out_lock;
478 }
479
3d80ede4 480 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
1da177e4
LT
481 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
482
579aa9ca 483 xfs_ilock(ip, XFS_ILOCK_EXCL);
1da177e4
LT
484 }
485
486 return 0;
487
488out_lock:
579aa9ca 489 xfs_ilock(ip, XFS_ILOCK_EXCL);
1da177e4
LT
490 ASSERT(error >= 0);
491 return error;
492}
493
494ssize_t /* bytes written, or (-) error */
495xfs_write(
993386c1 496 struct xfs_inode *xip,
1da177e4
LT
497 struct kiocb *iocb,
498 const struct iovec *iovp,
499 unsigned int nsegs,
500 loff_t *offset,
993386c1 501 int ioflags)
1da177e4
LT
502{
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;
1da177e4
LT
507 xfs_mount_t *mp;
508 ssize_t ret = 0, error = 0;
509 xfs_fsize_t isize, new_size;
1da177e4
LT
510 int iolock;
511 int eventsent = 0;
1da177e4
LT
512 size_t ocount = 0, count;
513 loff_t pos;
2a329631 514 int need_i_mutex;
1da177e4
LT
515
516 XFS_STATS_INC(xs_write_calls);
517
0ceb3314
DM
518 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
519 if (error)
520 return error;
1da177e4
LT
521
522 count = ocount;
523 pos = *offset;
524
525 if (count == 0)
526 return 0;
527
613d7043 528 mp = xip->i_mount;
1da177e4 529
b267ce99 530 xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
34327e13 531
1da177e4
LT
532 if (XFS_FORCED_SHUTDOWN(mp))
533 return -EIO;
534
1da177e4 535relock:
2a329631
LM
536 if (ioflags & IO_ISDIRECT) {
537 iolock = XFS_IOLOCK_SHARED;
2a329631
LM
538 need_i_mutex = 0;
539 } else {
1da177e4 540 iolock = XFS_IOLOCK_EXCL;
2a329631 541 need_i_mutex = 1;
1b1dcc1b 542 mutex_lock(&inode->i_mutex);
1da177e4
LT
543 }
544
545 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
546
1da177e4
LT
547start:
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);
a13828b1 552 goto out_unlock_mutex;
1da177e4
LT
553 }
554
eb9df39d 555 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
1da177e4 556 !(ioflags & IO_INVIS) && !eventsent)) {
1da177e4
LT
557 int dmflags = FILP_DELAY_FLAG(file);
558
a13828b1 559 if (need_i_mutex)
1b1dcc1b 560 dmflags |= DM_FLAGS_IMUX;
1da177e4
LT
561
562 xfs_iunlock(xip, XFS_ILOCK_EXCL);
bc4ac74a 563 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, xip,
126468b1 564 pos, count, dmflags, &iolock);
1da177e4 565 if (error) {
ba87ea69 566 goto out_unlock_internal;
1da177e4
LT
567 }
568 xfs_ilock(xip, XFS_ILOCK_EXCL);
569 eventsent = 1;
570
571 /*
c41564b5 572 * The iolock was dropped and reacquired in XFS_SEND_DATA
1da177e4
LT
573 * 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 */
71dfd5a3 578 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
1da177e4 579 goto start;
1da177e4
LT
580 }
581
71dfd5a3
LM
582 if (ioflags & IO_ISDIRECT) {
583 xfs_buftarg_t *target =
71ddabb9 584 XFS_IS_REALTIME_INODE(xip) ?
71dfd5a3
LM
585 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
dcf49cc5 592 if (!need_i_mutex && (mapping->nrpages || pos > xip->i_size)) {
71dfd5a3
LM
593 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
594 iolock = XFS_IOLOCK_EXCL;
71dfd5a3
LM
595 need_i_mutex = 1;
596 mutex_lock(&inode->i_mutex);
597 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
1da177e4
LT
598 goto start;
599 }
600 }
601
71dfd5a3
LM
602 new_size = pos + count;
603 if (new_size > xip->i_size)
613d7043 604 xip->i_new_size = new_size;
71dfd5a3 605
2234d54d 606 if (likely(!(ioflags & IO_INVIS)))
f9581b14 607 file_update_time(file);
1da177e4
LT
608
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
ba87ea69 618 if (pos > xip->i_size) {
541d7d3c 619 error = xfs_zero_eof(xip, pos, xip->i_size);
1da177e4 620 if (error) {
ba87ea69
LM
621 xfs_iunlock(xip, XFS_ILOCK_EXCL);
622 goto out_unlock_internal;
1da177e4
LT
623 }
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 */
633
634 if (((xip->i_d.di_mode & S_ISUID) ||
635 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
636 (S_ISGID | S_IXGRP))) &&
637 !capable(CAP_FSETID)) {
638 error = xfs_write_clear_setuid(xip);
639 if (likely(!error))
2f1936b8 640 error = -file_remove_suid(file);
1da177e4 641 if (unlikely(error)) {
ba87ea69 642 goto out_unlock_internal;
1da177e4
LT
643 }
644 }
645
1da177e4
LT
646 /* We can write back this queue in page reclaim */
647 current->backing_dev_info = mapping->backing_dev_info;
648
649 if ((ioflags & IO_ISDIRECT)) {
dcf49cc5 650 if (mapping->nrpages) {
2a329631 651 WARN_ON(need_i_mutex == 0);
739bfb2a 652 error = xfs_flushinval_pages(xip,
e6a4b37f 653 (pos & PAGE_CACHE_MASK),
1da177e4 654 -1, FI_REMAPF_LOCKED);
d3cf2094
LM
655 if (error)
656 goto out_unlock_internal;
1da177e4
LT
657 }
658
a13828b1 659 if (need_i_mutex) {
1da177e4 660 /* demote the lock now the cached pages are gone */
541d7d3c 661 xfs_ilock_demote(xip, XFS_IOLOCK_EXCL);
1b1dcc1b 662 mutex_unlock(&inode->i_mutex);
1da177e4
LT
663
664 iolock = XFS_IOLOCK_SHARED;
a13828b1 665 need_i_mutex = 0;
1da177e4
LT
666 }
667
0b1b213f 668 trace_xfs_file_direct_write(xip, count, *offset, ioflags);
1da177e4
LT
669 ret = generic_file_direct_write(iocb, iovp,
670 &segs, pos, offset, count, ocount);
671
672 /*
673 * direct-io write to a hole: fall through to buffered I/O
674 * for completing the rest of the request.
675 */
676 if (ret >= 0 && ret != count) {
677 XFS_STATS_ADD(xs_write_bytes, ret);
678
679 pos += ret;
680 count -= ret;
681
1da177e4
LT
682 ioflags &= ~IO_ISDIRECT;
683 xfs_iunlock(xip, iolock);
684 goto relock;
685 }
686 } else {
5825294e
DC
687 int enospc = 0;
688 ssize_t ret2 = 0;
689
690write_retry:
0b1b213f 691 trace_xfs_file_buffered_write(xip, count, *offset, ioflags);
5825294e 692 ret2 = generic_file_buffered_write(iocb, iovp, segs,
1da177e4 693 pos, offset, count, ret);
5825294e
DC
694 /*
695 * if we just got an ENOSPC, flush the inode now we
696 * aren't holding any page locks and retry *once*
697 */
698 if (ret2 == -ENOSPC && !enospc) {
699 error = xfs_flush_pages(xip, 0, -1, 0, FI_NONE);
700 if (error)
701 goto out_unlock_internal;
702 enospc = 1;
703 goto write_retry;
704 }
705 ret = ret2;
1da177e4
LT
706 }
707
708 current->backing_dev_info = NULL;
709
25051158
LM
710 isize = i_size_read(inode);
711 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
712 *offset = isize;
713
714 if (*offset > xip->i_size) {
715 xfs_ilock(xip, XFS_ILOCK_EXCL);
716 if (*offset > xip->i_size)
717 xip->i_size = *offset;
718 xfs_iunlock(xip, XFS_ILOCK_EXCL);
719 }
720
eb9df39d
CH
721 if (ret == -ENOSPC &&
722 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
126468b1 723 xfs_iunlock(xip, iolock);
a13828b1 724 if (need_i_mutex)
1b1dcc1b 725 mutex_unlock(&inode->i_mutex);
bc4ac74a
CH
726 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, xip,
727 DM_RIGHT_NULL, xip, DM_RIGHT_NULL, NULL, NULL,
1da177e4 728 0, 0, 0); /* Delay flag intentionally unused */
a13828b1 729 if (need_i_mutex)
1b1dcc1b 730 mutex_lock(&inode->i_mutex);
126468b1 731 xfs_ilock(xip, iolock);
ba87ea69
LM
732 if (error)
733 goto out_unlock_internal;
25051158 734 goto start;
1da177e4
LT
735 }
736
737 error = -ret;
738 if (ret <= 0)
739 goto out_unlock_internal;
740
741 XFS_STATS_ADD(xs_write_bytes, ret);
742
743 /* Handle various SYNC-type writes */
6b2f3d1f 744 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
13e6d5cd 745 loff_t end = pos + ret - 1;
5903c495 746 int error2;
126468b1
CH
747
748 xfs_iunlock(xip, iolock);
a13828b1 749 if (need_i_mutex)
1b1dcc1b 750 mutex_unlock(&inode->i_mutex);
13e6d5cd
CH
751
752 error2 = filemap_write_and_wait_range(mapping, pos, end);
1da177e4 753 if (!error)
5903c495 754 error = error2;
ba87ea69
LM
755 if (need_i_mutex)
756 mutex_lock(&inode->i_mutex);
126468b1 757 xfs_ilock(xip, iolock);
13e6d5cd
CH
758
759 error2 = xfs_fsync(xip);
5903c495
LM
760 if (!error)
761 error = error2;
1da177e4
LT
762 }
763
764 out_unlock_internal:
613d7043 765 if (xip->i_new_size) {
ba87ea69 766 xfs_ilock(xip, XFS_ILOCK_EXCL);
613d7043 767 xip->i_new_size = 0;
ba87ea69
LM
768 /*
769 * If this was a direct or synchronous I/O that failed (such
770 * as ENOSPC) then part of the I/O may have been written to
771 * disk before the error occured. In this case the on-disk
772 * file size may have been adjusted beyond the in-memory file
773 * size and now needs to be truncated back.
774 */
775 if (xip->i_d.di_size > xip->i_size)
776 xip->i_d.di_size = xip->i_size;
777 xfs_iunlock(xip, XFS_ILOCK_EXCL);
778 }
126468b1 779 xfs_iunlock(xip, iolock);
a13828b1
NS
780 out_unlock_mutex:
781 if (need_i_mutex)
1b1dcc1b 782 mutex_unlock(&inode->i_mutex);
1da177e4
LT
783 return -error;
784}
785
786/*
787 * All xfs metadata buffers except log state machine buffers
788 * get this attached as their b_bdstrat callback function.
789 * This is so that we can catch a buffer
790 * after prematurely unpinning it to forcibly shutdown the filesystem.
791 */
792int
793xfs_bdstrat_cb(struct xfs_buf *bp)
794{
15ac08a8 795 if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
0b1b213f 796 trace_xfs_bdstrat_shut(bp, _RET_IP_);
1da177e4
LT
797 /*
798 * Metadata write that didn't get logged but
799 * written delayed anyway. These aren't associated
800 * with a transaction, and can be ignored.
801 */
802 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
803 (XFS_BUF_ISREAD(bp)) == 0)
804 return (xfs_bioerror_relse(bp));
805 else
806 return (xfs_bioerror(bp));
807 }
15ac08a8
CH
808
809 xfs_buf_iorequest(bp);
810 return 0;
1da177e4
LT
811}
812
1da177e4 813/*
d64e31a2
DC
814 * Wrapper around bdstrat so that we can stop data from going to disk in case
815 * we are shutting down the filesystem. Typically user data goes thru this
816 * path; one of the exceptions is the superblock.
1da177e4 817 */
d64e31a2 818void
1da177e4
LT
819xfsbdstrat(
820 struct xfs_mount *mp,
821 struct xfs_buf *bp)
822{
823 ASSERT(mp);
d4055947 824 if (!XFS_FORCED_SHUTDOWN(mp)) {
ce8e922c 825 xfs_buf_iorequest(bp);
d4055947
DC
826 return;
827 }
1da177e4 828
0b1b213f 829 trace_xfs_bdstrat_shut(bp, _RET_IP_);
d64e31a2 830 xfs_bioerror_relse(bp);
1da177e4
LT
831}
832
833/*
834 * If the underlying (data/log/rt) device is readonly, there are some
835 * operations that cannot proceed.
836 */
837int
838xfs_dev_is_read_only(
839 xfs_mount_t *mp,
840 char *message)
841{
842 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
843 xfs_readonly_buftarg(mp->m_logdev_targp) ||
844 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
845 cmn_err(CE_NOTE,
846 "XFS: %s required on read-only device.", message);
847 cmn_err(CE_NOTE,
848 "XFS: write access unavailable, cannot proceed.");
849 return EROFS;
850 }
851 return 0;
852}
This page took 0.466118 seconds and 5 git commands to generate.