[XFS] call common xfs vnode-level helpers directly and remove vnode operations
[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"
993386c1 51#include "xfs_vnodeops.h"
1da177e4
LT
52
53#include <linux/capability.h>
54#include <linux/writeback.h>
55
56
57#if defined(XFS_RW_TRACE)
58void
59xfs_rw_enter_trace(
60 int tag,
61 xfs_iocore_t *io,
62 void *data,
63 size_t segs,
64 loff_t offset,
65 int ioflags)
66{
67 xfs_inode_t *ip = XFS_IO_INODE(io);
68
69 if (ip->i_rwtrace == NULL)
70 return;
71 ktrace_enter(ip->i_rwtrace,
72 (void *)(unsigned long)tag,
73 (void *)ip,
74 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
75 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
76 (void *)data,
77 (void *)((unsigned long)segs),
78 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
79 (void *)((unsigned long)(offset & 0xffffffff)),
80 (void *)((unsigned long)ioflags),
81 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
82 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
f1fdc848 83 (void *)((unsigned long)current_pid()),
1da177e4
LT
84 (void *)NULL,
85 (void *)NULL,
86 (void *)NULL,
87 (void *)NULL);
88}
89
90void
91xfs_inval_cached_trace(
92 xfs_iocore_t *io,
93 xfs_off_t offset,
94 xfs_off_t len,
95 xfs_off_t first,
96 xfs_off_t last)
97{
98 xfs_inode_t *ip = XFS_IO_INODE(io);
99
100 if (ip->i_rwtrace == NULL)
101 return;
102 ktrace_enter(ip->i_rwtrace,
103 (void *)(__psint_t)XFS_INVAL_CACHED,
104 (void *)ip,
105 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
106 (void *)((unsigned long)(offset & 0xffffffff)),
107 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
108 (void *)((unsigned long)(len & 0xffffffff)),
109 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
110 (void *)((unsigned long)(first & 0xffffffff)),
111 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
112 (void *)((unsigned long)(last & 0xffffffff)),
f1fdc848 113 (void *)((unsigned long)current_pid()),
1da177e4
LT
114 (void *)NULL,
115 (void *)NULL,
116 (void *)NULL,
117 (void *)NULL,
118 (void *)NULL);
119}
120#endif
121
122/*
123 * xfs_iozero
124 *
125 * xfs_iozero clears the specified range of buffer supplied,
126 * and marks all the affected blocks as valid and modified. If
127 * an affected block is not allocated, it will be allocated. If
128 * an affected block is not completely overwritten, and is not
129 * valid before the operation, it will be read from disk before
130 * being partially zeroed.
131 */
132STATIC int
133xfs_iozero(
134 struct inode *ip, /* inode */
135 loff_t pos, /* offset in file */
68160161 136 size_t count) /* size of data to zero */
1da177e4
LT
137{
138 unsigned bytes;
139 struct page *page;
140 struct address_space *mapping;
1da177e4
LT
141 int status;
142
143 mapping = ip->i_mapping;
144 do {
145 unsigned long index, offset;
146
147 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
148 index = pos >> PAGE_CACHE_SHIFT;
149 bytes = PAGE_CACHE_SIZE - offset;
150 if (bytes > count)
151 bytes = count;
152
153 status = -ENOMEM;
154 page = grab_cache_page(mapping, index);
155 if (!page)
156 break;
157
1da177e4
LT
158 status = mapping->a_ops->prepare_write(NULL, page, offset,
159 offset + bytes);
e7ff6aed 160 if (status)
1da177e4 161 goto unlock;
1da177e4 162
700716c8 163 zero_user_page(page, offset, bytes, KM_USER0);
e7ff6aed 164
1da177e4
LT
165 status = mapping->a_ops->commit_write(NULL, page, offset,
166 offset + bytes);
167 if (!status) {
168 pos += bytes;
169 count -= bytes;
1da177e4
LT
170 }
171
172unlock:
1da177e4
LT
173 unlock_page(page);
174 page_cache_release(page);
175 if (status)
176 break;
177 } while (count);
178
179 return (-status);
180}
181
1da177e4
LT
182ssize_t /* bytes read, or (-) error */
183xfs_read(
993386c1 184 xfs_inode_t *ip,
1da177e4
LT
185 struct kiocb *iocb,
186 const struct iovec *iovp,
187 unsigned int segs,
188 loff_t *offset,
993386c1 189 int ioflags)
1da177e4
LT
190{
191 struct file *file = iocb->ki_filp;
192 struct inode *inode = file->f_mapping->host;
993386c1
CH
193 bhv_vnode_t *vp = XFS_ITOV(ip);
194 xfs_mount_t *mp = ip->i_mount;
1da177e4 195 size_t size = 0;
d3cf2094 196 ssize_t ret = 0;
1da177e4 197 xfs_fsize_t n;
1da177e4
LT
198 unsigned long seg;
199
1da177e4
LT
200
201 XFS_STATS_INC(xs_read_calls);
202
203 /* START copy & waste from filemap.c */
204 for (seg = 0; seg < segs; seg++) {
205 const struct iovec *iv = &iovp[seg];
206
207 /*
208 * If any segment has a negative length, or the cumulative
209 * length ever wraps negative then return -EINVAL.
210 */
211 size += iv->iov_len;
212 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
213 return XFS_ERROR(-EINVAL);
214 }
215 /* END copy & waste from filemap.c */
216
217 if (unlikely(ioflags & IO_ISDIRECT)) {
218 xfs_buftarg_t *target =
219 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
220 mp->m_rtdev_targp : mp->m_ddev_targp;
ce8e922c
NS
221 if ((*offset & target->bt_smask) ||
222 (size & target->bt_smask)) {
ba87ea69 223 if (*offset == ip->i_size) {
1da177e4
LT
224 return (0);
225 }
226 return -XFS_ERROR(EINVAL);
227 }
228 }
229
230 n = XFS_MAXIOFFSET(mp) - *offset;
231 if ((n <= 0) || (size == 0))
232 return 0;
233
234 if (n < size)
235 size = n;
236
a13828b1 237 if (XFS_FORCED_SHUTDOWN(mp))
1da177e4 238 return -EIO;
1da177e4
LT
239
240 if (unlikely(ioflags & IO_ISDIRECT))
1b1dcc1b 241 mutex_lock(&inode->i_mutex);
1da177e4
LT
242 xfs_ilock(ip, XFS_IOLOCK_SHARED);
243
eb9df39d 244 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
8285fb58 245 bhv_vrwlock_t locktype = VRWLOCK_READ;
e1a40fa9 246 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
1da177e4 247
993386c1 248 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *offset, size,
e1a40fa9 249 dmflags, &locktype);
1da177e4
LT
250 if (ret) {
251 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
721259bc
LM
252 if (unlikely(ioflags & IO_ISDIRECT))
253 mutex_unlock(&inode->i_mutex);
254 return ret;
1da177e4
LT
255 }
256 }
257
edcd4bce
NS
258 if (unlikely(ioflags & IO_ISDIRECT)) {
259 if (VN_CACHED(vp))
739bfb2a
CH
260 ret = xfs_flushinval_pages(ip,
261 ctooff(offtoct(*offset)),
262 -1, FI_REMAPF_LOCKED);
721259bc 263 mutex_unlock(&inode->i_mutex);
d3cf2094
LM
264 if (ret) {
265 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
266 return ret;
267 }
edcd4bce 268 }
721259bc 269
1da177e4
LT
270 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
271 (void *)iovp, segs, *offset, ioflags);
543ade1f
BP
272
273 iocb->ki_pos = *offset;
274 ret = generic_file_aio_read(iocb, iovp, segs, *offset);
1da177e4
LT
275 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
276 ret = wait_on_sync_kiocb(iocb);
277 if (ret > 0)
278 XFS_STATS_ADD(xs_read_bytes, ret);
279
280 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
1da177e4
LT
281 return ret;
282}
283
1b895840
NS
284ssize_t
285xfs_splice_read(
993386c1 286 xfs_inode_t *ip,
1b895840 287 struct file *infilp,
cbb7e577 288 loff_t *ppos,
3a326a2c 289 struct pipe_inode_info *pipe,
1b895840
NS
290 size_t count,
291 int flags,
993386c1 292 int ioflags)
1b895840 293{
993386c1 294 bhv_vnode_t *vp = XFS_ITOV(ip);
1b895840
NS
295 xfs_mount_t *mp = ip->i_mount;
296 ssize_t ret;
1da177e4 297
1b895840
NS
298 XFS_STATS_INC(xs_read_calls);
299 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
300 return -EIO;
301
302 xfs_ilock(ip, XFS_IOLOCK_SHARED);
303
eb9df39d 304 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
8285fb58 305 bhv_vrwlock_t locktype = VRWLOCK_READ;
1b895840
NS
306 int error;
307
993386c1 308 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *ppos, count,
1b895840
NS
309 FILP_DELAY_FLAG(infilp), &locktype);
310 if (error) {
311 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
312 return -error;
313 }
314 }
315 xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, &ip->i_iocore,
cbb7e577
JA
316 pipe, count, *ppos, ioflags);
317 ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
1da177e4
LT
318 if (ret > 0)
319 XFS_STATS_ADD(xs_read_bytes, ret);
320
1b895840
NS
321 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
322 return ret;
323}
324
325ssize_t
326xfs_splice_write(
993386c1 327 xfs_inode_t *ip,
3a326a2c 328 struct pipe_inode_info *pipe,
1b895840 329 struct file *outfilp,
cbb7e577 330 loff_t *ppos,
1b895840
NS
331 size_t count,
332 int flags,
993386c1 333 int ioflags)
1b895840 334{
993386c1 335 bhv_vnode_t *vp = XFS_ITOV(ip);
1b895840 336 xfs_mount_t *mp = ip->i_mount;
ba87ea69 337 xfs_iocore_t *io = &ip->i_iocore;
1b895840 338 ssize_t ret;
0a8d17d0 339 struct inode *inode = outfilp->f_mapping->host;
ba87ea69 340 xfs_fsize_t isize, new_size;
1b895840
NS
341
342 XFS_STATS_INC(xs_write_calls);
343 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
344 return -EIO;
345
346 xfs_ilock(ip, XFS_IOLOCK_EXCL);
347
eb9df39d 348 if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
8285fb58 349 bhv_vrwlock_t locktype = VRWLOCK_WRITE;
1b895840
NS
350 int error;
351
993386c1 352 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp, *ppos, count,
1b895840
NS
353 FILP_DELAY_FLAG(outfilp), &locktype);
354 if (error) {
355 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
356 return -error;
357 }
358 }
ba87ea69
LM
359
360 new_size = *ppos + count;
361
362 xfs_ilock(ip, XFS_ILOCK_EXCL);
363 if (new_size > ip->i_size)
364 io->io_new_size = new_size;
365 xfs_iunlock(ip, XFS_ILOCK_EXCL);
366
1b895840 367 xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, &ip->i_iocore,
cbb7e577
JA
368 pipe, count, *ppos, ioflags);
369 ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
1b895840
NS
370 if (ret > 0)
371 XFS_STATS_ADD(xs_write_bytes, ret);
372
0a8d17d0
DC
373 isize = i_size_read(inode);
374 if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
375 *ppos = isize;
376
ba87ea69 377 if (*ppos > ip->i_size) {
0a8d17d0 378 xfs_ilock(ip, XFS_ILOCK_EXCL);
ba87ea69
LM
379 if (*ppos > ip->i_size)
380 ip->i_size = *ppos;
381 xfs_iunlock(ip, XFS_ILOCK_EXCL);
382 }
383
384 if (io->io_new_size) {
385 xfs_ilock(ip, XFS_ILOCK_EXCL);
386 io->io_new_size = 0;
387 if (ip->i_d.di_size > ip->i_size)
388 ip->i_d.di_size = ip->i_size;
0a8d17d0
DC
389 xfs_iunlock(ip, XFS_ILOCK_EXCL);
390 }
1b895840 391 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1da177e4
LT
392 return ret;
393}
394
395/*
396 * This routine is called to handle zeroing any space in the last
397 * block of the file that is beyond the EOF. We do this since the
398 * size is being increased without writing anything to that block
399 * and we don't want anyone to read the garbage on the disk.
400 */
401STATIC int /* error (positive) */
402xfs_zero_last_block(
403 struct inode *ip,
404 xfs_iocore_t *io,
68160161
LM
405 xfs_fsize_t offset,
406 xfs_fsize_t isize)
1da177e4
LT
407{
408 xfs_fileoff_t last_fsb;
1b895840 409 xfs_mount_t *mp = io->io_mount;
1da177e4
LT
410 int nimaps;
411 int zero_offset;
412 int zero_len;
1da177e4
LT
413 int error = 0;
414 xfs_bmbt_irec_t imap;
1da177e4
LT
415
416 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
1da177e4 417
24ee8088
ES
418 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
419 if (zero_offset == 0) {
1da177e4
LT
420 /*
421 * There are no extra bytes in the last block on disk to
422 * zero, so return.
423 */
424 return 0;
425 }
426
427 last_fsb = XFS_B_TO_FSBT(mp, isize);
428 nimaps = 1;
429 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
3e57ecf6 430 &nimaps, NULL, NULL);
1da177e4
LT
431 if (error) {
432 return error;
433 }
434 ASSERT(nimaps > 0);
435 /*
436 * If the block underlying isize is just a hole, then there
437 * is nothing to zero.
438 */
439 if (imap.br_startblock == HOLESTARTBLOCK) {
440 return 0;
441 }
442 /*
443 * Zero the part of the last block beyond the EOF, and write it
444 * out sync. We need to drop the ilock while we do this so we
445 * don't deadlock when the buffer cache calls back to us.
446 */
447 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
1da177e4 448
24ee8088 449 zero_len = mp->m_sb.sb_blocksize - zero_offset;
68160161
LM
450 if (isize + zero_len > offset)
451 zero_len = offset - isize;
452 error = xfs_iozero(ip, isize, zero_len);
1da177e4
LT
453
454 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
455 ASSERT(error >= 0);
456 return error;
457}
458
459/*
460 * Zero any on disk space between the current EOF and the new,
461 * larger EOF. This handles the normal case of zeroing the remainder
462 * of the last block in the file and the unusual case of zeroing blocks
463 * out beyond the size of the file. This second case only happens
464 * with fixed size extents and when the system crashes before the inode
465 * size was updated but after blocks were allocated. If fill is set,
466 * then any holes in the range are filled and zeroed. If not, the holes
467 * are left alone as holes.
468 */
469
470int /* error (positive) */
471xfs_zero_eof(
67fcaa73 472 bhv_vnode_t *vp,
1da177e4
LT
473 xfs_iocore_t *io,
474 xfs_off_t offset, /* starting I/O offset */
68160161 475 xfs_fsize_t isize) /* current inode size */
1da177e4 476{
ec86dc02 477 struct inode *ip = vn_to_inode(vp);
1da177e4
LT
478 xfs_fileoff_t start_zero_fsb;
479 xfs_fileoff_t end_zero_fsb;
1da177e4
LT
480 xfs_fileoff_t zero_count_fsb;
481 xfs_fileoff_t last_fsb;
68160161
LM
482 xfs_fileoff_t zero_off;
483 xfs_fsize_t zero_len;
1b895840 484 xfs_mount_t *mp = io->io_mount;
1da177e4
LT
485 int nimaps;
486 int error = 0;
487 xfs_bmbt_irec_t imap;
1da177e4
LT
488
489 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
490 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
24ee8088 491 ASSERT(offset > isize);
1da177e4 492
1da177e4
LT
493 /*
494 * First handle zeroing the block on which isize resides.
495 * We only zero a part of that block so it is handled specially.
496 */
68160161 497 error = xfs_zero_last_block(ip, io, offset, isize);
1da177e4
LT
498 if (error) {
499 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
500 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
501 return error;
502 }
503
504 /*
505 * Calculate the range between the new size and the old
506 * where blocks needing to be zeroed may exist. To get the
507 * block where the last byte in the file currently resides,
508 * we need to subtract one from the size and truncate back
509 * to a block boundary. We subtract 1 in case the size is
510 * exactly on a block boundary.
511 */
512 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
513 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
514 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
515 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
516 if (last_fsb == end_zero_fsb) {
517 /*
518 * The size was only incremented on its last block.
519 * We took care of that above, so just return.
520 */
521 return 0;
522 }
523
524 ASSERT(start_zero_fsb <= end_zero_fsb);
1da177e4
LT
525 while (start_zero_fsb <= end_zero_fsb) {
526 nimaps = 1;
527 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
528 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
3e57ecf6 529 0, NULL, 0, &imap, &nimaps, NULL, NULL);
1da177e4
LT
530 if (error) {
531 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
532 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
533 return error;
534 }
535 ASSERT(nimaps > 0);
536
537 if (imap.br_state == XFS_EXT_UNWRITTEN ||
538 imap.br_startblock == HOLESTARTBLOCK) {
539 /*
540 * This loop handles initializing pages that were
541 * partially initialized by the code below this
542 * loop. It basically zeroes the part of the page
543 * that sits on a hole and sets the page as P_HOLE
544 * and calls remapf if it is a mapped file.
545 */
24ee8088 546 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
1da177e4
LT
547 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
548 continue;
549 }
550
551 /*
3d80ede4 552 * There are blocks we need to zero.
1da177e4
LT
553 * Drop the inode lock while we're doing the I/O.
554 * We'll still have the iolock to protect us.
555 */
556 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
557
68160161
LM
558 zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
559 zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
560
561 if ((zero_off + zero_len) > offset)
562 zero_len = offset - zero_off;
563
564 error = xfs_iozero(ip, zero_off, zero_len);
1da177e4
LT
565 if (error) {
566 goto out_lock;
567 }
568
3d80ede4 569 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
1da177e4
LT
570 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
571
572 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
573 }
574
575 return 0;
576
577out_lock:
578
579 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
580 ASSERT(error >= 0);
581 return error;
582}
583
584ssize_t /* bytes written, or (-) error */
585xfs_write(
993386c1 586 struct xfs_inode *xip,
1da177e4
LT
587 struct kiocb *iocb,
588 const struct iovec *iovp,
589 unsigned int nsegs,
590 loff_t *offset,
993386c1 591 int ioflags)
1da177e4
LT
592{
593 struct file *file = iocb->ki_filp;
594 struct address_space *mapping = file->f_mapping;
595 struct inode *inode = mapping->host;
993386c1 596 bhv_vnode_t *vp = XFS_ITOV(xip);
1da177e4 597 unsigned long segs = nsegs;
1da177e4
LT
598 xfs_mount_t *mp;
599 ssize_t ret = 0, error = 0;
600 xfs_fsize_t isize, new_size;
601 xfs_iocore_t *io;
1da177e4
LT
602 int iolock;
603 int eventsent = 0;
8285fb58 604 bhv_vrwlock_t locktype;
1da177e4
LT
605 size_t ocount = 0, count;
606 loff_t pos;
2a329631 607 int need_i_mutex;
1da177e4
LT
608
609 XFS_STATS_INC(xs_write_calls);
610
0ceb3314
DM
611 error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
612 if (error)
613 return error;
1da177e4
LT
614
615 count = ocount;
616 pos = *offset;
617
618 if (count == 0)
619 return 0;
620
621 io = &xip->i_iocore;
622 mp = io->io_mount;
623
34327e13
NS
624 vfs_wait_for_freeze(vp->v_vfsp, SB_FREEZE_WRITE);
625
1da177e4
LT
626 if (XFS_FORCED_SHUTDOWN(mp))
627 return -EIO;
628
1da177e4 629relock:
2a329631
LM
630 if (ioflags & IO_ISDIRECT) {
631 iolock = XFS_IOLOCK_SHARED;
632 locktype = VRWLOCK_WRITE_DIRECT;
633 need_i_mutex = 0;
634 } else {
1da177e4
LT
635 iolock = XFS_IOLOCK_EXCL;
636 locktype = VRWLOCK_WRITE;
2a329631 637 need_i_mutex = 1;
1b1dcc1b 638 mutex_lock(&inode->i_mutex);
1da177e4
LT
639 }
640
641 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
642
1da177e4
LT
643start:
644 error = -generic_write_checks(file, &pos, &count,
645 S_ISBLK(inode->i_mode));
646 if (error) {
647 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
a13828b1 648 goto out_unlock_mutex;
1da177e4
LT
649 }
650
eb9df39d 651 if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
1da177e4 652 !(ioflags & IO_INVIS) && !eventsent)) {
1da177e4
LT
653 int dmflags = FILP_DELAY_FLAG(file);
654
a13828b1 655 if (need_i_mutex)
1b1dcc1b 656 dmflags |= DM_FLAGS_IMUX;
1da177e4
LT
657
658 xfs_iunlock(xip, XFS_ILOCK_EXCL);
659 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
660 pos, count,
661 dmflags, &locktype);
662 if (error) {
ba87ea69 663 goto out_unlock_internal;
1da177e4
LT
664 }
665 xfs_ilock(xip, XFS_ILOCK_EXCL);
666 eventsent = 1;
667
668 /*
c41564b5 669 * The iolock was dropped and reacquired in XFS_SEND_DATA
1da177e4
LT
670 * so we have to recheck the size when appending.
671 * We will only "goto start;" once, since having sent the
672 * event prevents another call to XFS_SEND_DATA, which is
673 * what allows the size to change in the first place.
674 */
71dfd5a3 675 if ((file->f_flags & O_APPEND) && pos != xip->i_size)
1da177e4 676 goto start;
1da177e4
LT
677 }
678
71dfd5a3
LM
679 if (ioflags & IO_ISDIRECT) {
680 xfs_buftarg_t *target =
681 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
682 mp->m_rtdev_targp : mp->m_ddev_targp;
683
684 if ((pos & target->bt_smask) || (count & target->bt_smask)) {
685 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
686 return XFS_ERROR(-EINVAL);
687 }
688
689 if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) {
690 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
691 iolock = XFS_IOLOCK_EXCL;
692 locktype = VRWLOCK_WRITE;
693 need_i_mutex = 1;
694 mutex_lock(&inode->i_mutex);
695 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
1da177e4
LT
696 goto start;
697 }
698 }
699
71dfd5a3
LM
700 new_size = pos + count;
701 if (new_size > xip->i_size)
702 io->io_new_size = new_size;
703
4aeb664c 704 if (likely(!(ioflags & IO_INVIS))) {
870f4817 705 file_update_time(file);
4aeb664c
NS
706 xfs_ichgtime_fast(xip, inode,
707 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1da177e4
LT
708 }
709
710 /*
711 * If the offset is beyond the size of the file, we have a couple
712 * of things to do. First, if there is already space allocated
713 * we need to either create holes or zero the disk or ...
714 *
715 * If there is a page where the previous size lands, we need
716 * to zero it out up to the new size.
717 */
718
ba87ea69 719 if (pos > xip->i_size) {
993386c1 720 error = xfs_zero_eof(vp, io, pos, xip->i_size);
1da177e4 721 if (error) {
ba87ea69
LM
722 xfs_iunlock(xip, XFS_ILOCK_EXCL);
723 goto out_unlock_internal;
1da177e4
LT
724 }
725 }
726 xfs_iunlock(xip, XFS_ILOCK_EXCL);
727
728 /*
729 * If we're writing the file then make sure to clear the
730 * setuid and setgid bits if the process is not being run
731 * by root. This keeps people from modifying setuid and
732 * setgid binaries.
733 */
734
735 if (((xip->i_d.di_mode & S_ISUID) ||
736 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
737 (S_ISGID | S_IXGRP))) &&
738 !capable(CAP_FSETID)) {
739 error = xfs_write_clear_setuid(xip);
740 if (likely(!error))
e678fb0d 741 error = -remove_suid(file->f_path.dentry);
1da177e4 742 if (unlikely(error)) {
ba87ea69 743 goto out_unlock_internal;
1da177e4
LT
744 }
745 }
746
747retry:
748 /* We can write back this queue in page reclaim */
749 current->backing_dev_info = mapping->backing_dev_info;
750
751 if ((ioflags & IO_ISDIRECT)) {
2a329631
LM
752 if (VN_CACHED(vp)) {
753 WARN_ON(need_i_mutex == 0);
1da177e4
LT
754 xfs_inval_cached_trace(io, pos, -1,
755 ctooff(offtoct(pos)), -1);
739bfb2a
CH
756 error = xfs_flushinval_pages(xip,
757 ctooff(offtoct(pos)),
1da177e4 758 -1, FI_REMAPF_LOCKED);
d3cf2094
LM
759 if (error)
760 goto out_unlock_internal;
1da177e4
LT
761 }
762
a13828b1 763 if (need_i_mutex) {
1da177e4
LT
764 /* demote the lock now the cached pages are gone */
765 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
1b1dcc1b 766 mutex_unlock(&inode->i_mutex);
1da177e4
LT
767
768 iolock = XFS_IOLOCK_SHARED;
769 locktype = VRWLOCK_WRITE_DIRECT;
a13828b1 770 need_i_mutex = 0;
1da177e4
LT
771 }
772
773 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
774 *offset, ioflags);
775 ret = generic_file_direct_write(iocb, iovp,
776 &segs, pos, offset, count, ocount);
777
778 /*
779 * direct-io write to a hole: fall through to buffered I/O
780 * for completing the rest of the request.
781 */
782 if (ret >= 0 && ret != count) {
783 XFS_STATS_ADD(xs_write_bytes, ret);
784
785 pos += ret;
786 count -= ret;
787
1da177e4
LT
788 ioflags &= ~IO_ISDIRECT;
789 xfs_iunlock(xip, iolock);
790 goto relock;
791 }
792 } else {
793 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
794 *offset, ioflags);
795 ret = generic_file_buffered_write(iocb, iovp, segs,
796 pos, offset, count, ret);
797 }
798
799 current->backing_dev_info = NULL;
800
801 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
802 ret = wait_on_sync_kiocb(iocb);
803
eb9df39d
CH
804 if (ret == -ENOSPC &&
805 DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
993386c1 806 xfs_rwunlock(xip, locktype);
a13828b1 807 if (need_i_mutex)
1b1dcc1b 808 mutex_unlock(&inode->i_mutex);
1da177e4
LT
809 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
810 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
811 0, 0, 0); /* Delay flag intentionally unused */
a13828b1 812 if (need_i_mutex)
1b1dcc1b 813 mutex_lock(&inode->i_mutex);
993386c1 814 xfs_rwlock(xip, locktype);
ba87ea69
LM
815 if (error)
816 goto out_unlock_internal;
817 pos = xip->i_size;
1da177e4
LT
818 ret = 0;
819 goto retry;
820 }
821
68bdb6ea
YL
822 isize = i_size_read(inode);
823 if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
824 *offset = isize;
825
ba87ea69 826 if (*offset > xip->i_size) {
1da177e4 827 xfs_ilock(xip, XFS_ILOCK_EXCL);
ba87ea69
LM
828 if (*offset > xip->i_size)
829 xip->i_size = *offset;
1da177e4
LT
830 xfs_iunlock(xip, XFS_ILOCK_EXCL);
831 }
832
833 error = -ret;
834 if (ret <= 0)
835 goto out_unlock_internal;
836
837 XFS_STATS_ADD(xs_write_bytes, ret);
838
839 /* Handle various SYNC-type writes */
840 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
1e69dd0e
NS
841 error = xfs_write_sync_logforce(mp, xip);
842 if (error)
843 goto out_unlock_internal;
a13828b1 844
993386c1 845 xfs_rwunlock(xip, locktype);
a13828b1 846 if (need_i_mutex)
1b1dcc1b 847 mutex_unlock(&inode->i_mutex);
1da177e4
LT
848
849 error = sync_page_range(inode, mapping, pos, ret);
850 if (!error)
ba87ea69
LM
851 error = -ret;
852 if (need_i_mutex)
853 mutex_lock(&inode->i_mutex);
993386c1 854 xfs_rwlock(xip, locktype);
1da177e4
LT
855 }
856
857 out_unlock_internal:
ba87ea69
LM
858 if (io->io_new_size) {
859 xfs_ilock(xip, XFS_ILOCK_EXCL);
860 io->io_new_size = 0;
861 /*
862 * If this was a direct or synchronous I/O that failed (such
863 * as ENOSPC) then part of the I/O may have been written to
864 * disk before the error occured. In this case the on-disk
865 * file size may have been adjusted beyond the in-memory file
866 * size and now needs to be truncated back.
867 */
868 if (xip->i_d.di_size > xip->i_size)
869 xip->i_d.di_size = xip->i_size;
870 xfs_iunlock(xip, XFS_ILOCK_EXCL);
871 }
993386c1 872 xfs_rwunlock(xip, locktype);
a13828b1
NS
873 out_unlock_mutex:
874 if (need_i_mutex)
1b1dcc1b 875 mutex_unlock(&inode->i_mutex);
1da177e4
LT
876 return -error;
877}
878
879/*
880 * All xfs metadata buffers except log state machine buffers
881 * get this attached as their b_bdstrat callback function.
882 * This is so that we can catch a buffer
883 * after prematurely unpinning it to forcibly shutdown the filesystem.
884 */
885int
886xfs_bdstrat_cb(struct xfs_buf *bp)
887{
888 xfs_mount_t *mp;
889
890 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
891 if (!XFS_FORCED_SHUTDOWN(mp)) {
ce8e922c 892 xfs_buf_iorequest(bp);
1da177e4
LT
893 return 0;
894 } else {
895 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
896 /*
897 * Metadata write that didn't get logged but
898 * written delayed anyway. These aren't associated
899 * with a transaction, and can be ignored.
900 */
901 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
902 (XFS_BUF_ISREAD(bp)) == 0)
903 return (xfs_bioerror_relse(bp));
904 else
905 return (xfs_bioerror(bp));
906 }
907}
908
909
910int
993386c1
CH
911xfs_bmap(
912 xfs_inode_t *ip,
1da177e4
LT
913 xfs_off_t offset,
914 ssize_t count,
915 int flags,
916 xfs_iomap_t *iomapp,
917 int *niomaps)
918{
1da177e4
LT
919 xfs_iocore_t *io = &ip->i_iocore;
920
921 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
922 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
923 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
924
925 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
926}
927
928/*
929 * Wrapper around bdstrat so that we can stop data
930 * from going to disk in case we are shutting down the filesystem.
931 * Typically user data goes thru this path; one of the exceptions
932 * is the superblock.
933 */
934int
935xfsbdstrat(
936 struct xfs_mount *mp,
937 struct xfs_buf *bp)
938{
939 ASSERT(mp);
940 if (!XFS_FORCED_SHUTDOWN(mp)) {
941 /* Grio redirection would go here
942 * if (XFS_BUF_IS_GRIO(bp)) {
943 */
944
ce8e922c 945 xfs_buf_iorequest(bp);
1da177e4
LT
946 return 0;
947 }
948
949 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
950 return (xfs_bioerror_relse(bp));
951}
952
953/*
954 * If the underlying (data/log/rt) device is readonly, there are some
955 * operations that cannot proceed.
956 */
957int
958xfs_dev_is_read_only(
959 xfs_mount_t *mp,
960 char *message)
961{
962 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
963 xfs_readonly_buftarg(mp->m_logdev_targp) ||
964 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
965 cmn_err(CE_NOTE,
966 "XFS: %s required on read-only device.", message);
967 cmn_err(CE_NOTE,
968 "XFS: write access unavailable, cannot proceed.");
969 return EROFS;
970 }
971 return 0;
972}
This page took 0.29992 seconds and 5 git commands to generate.