]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/xfs/linux-2.6/xfs_lrw.c
c73d3c18882cf46fc0082c45513f456b48f39218
[mirror_ubuntu-artful-kernel.git] / fs / xfs / linux-2.6 / xfs_lrw.c
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir.h"
27 #include "xfs_dir2.h"
28 #include "xfs_alloc.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_quota.h"
31 #include "xfs_mount.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir_sf.h"
36 #include "xfs_dir2_sf.h"
37 #include "xfs_attr_sf.h"
38 #include "xfs_dinode.h"
39 #include "xfs_inode.h"
40 #include "xfs_bmap.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_rtalloc.h"
44 #include "xfs_error.h"
45 #include "xfs_itable.h"
46 #include "xfs_rw.h"
47 #include "xfs_acl.h"
48 #include "xfs_cap.h"
49 #include "xfs_mac.h"
50 #include "xfs_attr.h"
51 #include "xfs_inode_item.h"
52 #include "xfs_buf_item.h"
53 #include "xfs_utils.h"
54 #include "xfs_iomap.h"
55
56 #include <linux/capability.h>
57 #include <linux/writeback.h>
58
59
60 #if defined(XFS_RW_TRACE)
61 void
62 xfs_rw_enter_trace(
63 int tag,
64 xfs_iocore_t *io,
65 void *data,
66 size_t segs,
67 loff_t offset,
68 int ioflags)
69 {
70 xfs_inode_t *ip = XFS_IO_INODE(io);
71
72 if (ip->i_rwtrace == NULL)
73 return;
74 ktrace_enter(ip->i_rwtrace,
75 (void *)(unsigned long)tag,
76 (void *)ip,
77 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
78 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
79 (void *)data,
80 (void *)((unsigned long)segs),
81 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
82 (void *)((unsigned long)(offset & 0xffffffff)),
83 (void *)((unsigned long)ioflags),
84 (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)),
85 (void *)((unsigned long)(io->io_new_size & 0xffffffff)),
86 (void *)NULL,
87 (void *)NULL,
88 (void *)NULL,
89 (void *)NULL,
90 (void *)NULL);
91 }
92
93 void
94 xfs_inval_cached_trace(
95 xfs_iocore_t *io,
96 xfs_off_t offset,
97 xfs_off_t len,
98 xfs_off_t first,
99 xfs_off_t last)
100 {
101 xfs_inode_t *ip = XFS_IO_INODE(io);
102
103 if (ip->i_rwtrace == NULL)
104 return;
105 ktrace_enter(ip->i_rwtrace,
106 (void *)(__psint_t)XFS_INVAL_CACHED,
107 (void *)ip,
108 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
109 (void *)((unsigned long)(offset & 0xffffffff)),
110 (void *)((unsigned long)((len >> 32) & 0xffffffff)),
111 (void *)((unsigned long)(len & 0xffffffff)),
112 (void *)((unsigned long)((first >> 32) & 0xffffffff)),
113 (void *)((unsigned long)(first & 0xffffffff)),
114 (void *)((unsigned long)((last >> 32) & 0xffffffff)),
115 (void *)((unsigned long)(last & 0xffffffff)),
116 (void *)NULL,
117 (void *)NULL,
118 (void *)NULL,
119 (void *)NULL,
120 (void *)NULL,
121 (void *)NULL);
122 }
123 #endif
124
125 /*
126 * xfs_iozero
127 *
128 * xfs_iozero clears the specified range of buffer supplied,
129 * and marks all the affected blocks as valid and modified. If
130 * an affected block is not allocated, it will be allocated. If
131 * an affected block is not completely overwritten, and is not
132 * valid before the operation, it will be read from disk before
133 * being partially zeroed.
134 */
135 STATIC int
136 xfs_iozero(
137 struct inode *ip, /* inode */
138 loff_t pos, /* offset in file */
139 size_t count, /* size of data to zero */
140 loff_t end_size) /* max file size to set */
141 {
142 unsigned bytes;
143 struct page *page;
144 struct address_space *mapping;
145 char *kaddr;
146 int status;
147
148 mapping = ip->i_mapping;
149 do {
150 unsigned long index, offset;
151
152 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
153 index = pos >> PAGE_CACHE_SHIFT;
154 bytes = PAGE_CACHE_SIZE - offset;
155 if (bytes > count)
156 bytes = count;
157
158 status = -ENOMEM;
159 page = grab_cache_page(mapping, index);
160 if (!page)
161 break;
162
163 kaddr = kmap(page);
164 status = mapping->a_ops->prepare_write(NULL, page, offset,
165 offset + bytes);
166 if (status) {
167 goto unlock;
168 }
169
170 memset((void *) (kaddr + offset), 0, bytes);
171 flush_dcache_page(page);
172 status = mapping->a_ops->commit_write(NULL, page, offset,
173 offset + bytes);
174 if (!status) {
175 pos += bytes;
176 count -= bytes;
177 if (pos > i_size_read(ip))
178 i_size_write(ip, pos < end_size ? pos : end_size);
179 }
180
181 unlock:
182 kunmap(page);
183 unlock_page(page);
184 page_cache_release(page);
185 if (status)
186 break;
187 } while (count);
188
189 return (-status);
190 }
191
192 ssize_t /* bytes read, or (-) error */
193 xfs_read(
194 bhv_desc_t *bdp,
195 struct kiocb *iocb,
196 const struct iovec *iovp,
197 unsigned int segs,
198 loff_t *offset,
199 int ioflags,
200 cred_t *credp)
201 {
202 struct file *file = iocb->ki_filp;
203 struct inode *inode = file->f_mapping->host;
204 size_t size = 0;
205 ssize_t ret;
206 xfs_fsize_t n;
207 xfs_inode_t *ip;
208 xfs_mount_t *mp;
209 vnode_t *vp;
210 unsigned long seg;
211
212 ip = XFS_BHVTOI(bdp);
213 vp = BHV_TO_VNODE(bdp);
214 mp = ip->i_mount;
215
216 XFS_STATS_INC(xs_read_calls);
217
218 /* START copy & waste from filemap.c */
219 for (seg = 0; seg < segs; seg++) {
220 const struct iovec *iv = &iovp[seg];
221
222 /*
223 * If any segment has a negative length, or the cumulative
224 * length ever wraps negative then return -EINVAL.
225 */
226 size += iv->iov_len;
227 if (unlikely((ssize_t)(size|iv->iov_len) < 0))
228 return XFS_ERROR(-EINVAL);
229 }
230 /* END copy & waste from filemap.c */
231
232 if (unlikely(ioflags & IO_ISDIRECT)) {
233 xfs_buftarg_t *target =
234 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
235 mp->m_rtdev_targp : mp->m_ddev_targp;
236 if ((*offset & target->pbr_smask) ||
237 (size & target->pbr_smask)) {
238 if (*offset == ip->i_d.di_size) {
239 return (0);
240 }
241 return -XFS_ERROR(EINVAL);
242 }
243 }
244
245 n = XFS_MAXIOFFSET(mp) - *offset;
246 if ((n <= 0) || (size == 0))
247 return 0;
248
249 if (n < size)
250 size = n;
251
252 if (XFS_FORCED_SHUTDOWN(mp)) {
253 return -EIO;
254 }
255
256 if (unlikely(ioflags & IO_ISDIRECT))
257 mutex_lock(&inode->i_mutex);
258 xfs_ilock(ip, XFS_IOLOCK_SHARED);
259
260 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
261 !(ioflags & IO_INVIS)) {
262 vrwlock_t locktype = VRWLOCK_READ;
263 int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
264
265 ret = -XFS_SEND_DATA(mp, DM_EVENT_READ,
266 BHV_TO_VNODE(bdp), *offset, size,
267 dmflags, &locktype);
268 if (ret) {
269 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
270 goto unlock_isem;
271 }
272 }
273
274 xfs_rw_enter_trace(XFS_READ_ENTER, &ip->i_iocore,
275 (void *)iovp, segs, *offset, ioflags);
276 ret = __generic_file_aio_read(iocb, iovp, segs, offset);
277 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
278 ret = wait_on_sync_kiocb(iocb);
279 if (ret > 0)
280 XFS_STATS_ADD(xs_read_bytes, ret);
281
282 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
283
284 if (likely(!(ioflags & IO_INVIS)))
285 xfs_ichgtime_fast(ip, inode, XFS_ICHGTIME_ACC);
286
287 unlock_isem:
288 if (unlikely(ioflags & IO_ISDIRECT))
289 mutex_unlock(&inode->i_mutex);
290 return ret;
291 }
292
293 ssize_t
294 xfs_sendfile(
295 bhv_desc_t *bdp,
296 struct file *filp,
297 loff_t *offset,
298 int ioflags,
299 size_t count,
300 read_actor_t actor,
301 void *target,
302 cred_t *credp)
303 {
304 ssize_t ret;
305 xfs_fsize_t n;
306 xfs_inode_t *ip;
307 xfs_mount_t *mp;
308 vnode_t *vp;
309
310 ip = XFS_BHVTOI(bdp);
311 vp = BHV_TO_VNODE(bdp);
312 mp = ip->i_mount;
313
314 XFS_STATS_INC(xs_read_calls);
315
316 n = XFS_MAXIOFFSET(mp) - *offset;
317 if ((n <= 0) || (count == 0))
318 return 0;
319
320 if (n < count)
321 count = n;
322
323 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
324 return -EIO;
325
326 xfs_ilock(ip, XFS_IOLOCK_SHARED);
327
328 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) &&
329 (!(ioflags & IO_INVIS))) {
330 vrwlock_t locktype = VRWLOCK_READ;
331 int error;
332
333 error = XFS_SEND_DATA(mp, DM_EVENT_READ, BHV_TO_VNODE(bdp), *offset, count,
334 FILP_DELAY_FLAG(filp), &locktype);
335 if (error) {
336 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
337 return -error;
338 }
339 }
340 xfs_rw_enter_trace(XFS_SENDFILE_ENTER, &ip->i_iocore,
341 (void *)(unsigned long)target, count, *offset, ioflags);
342 ret = generic_file_sendfile(filp, offset, count, actor, target);
343
344 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
345
346 if (ret > 0)
347 XFS_STATS_ADD(xs_read_bytes, ret);
348
349 if (likely(!(ioflags & IO_INVIS)))
350 xfs_ichgtime_fast(ip, LINVFS_GET_IP(vp), XFS_ICHGTIME_ACC);
351
352 return ret;
353 }
354
355 /*
356 * This routine is called to handle zeroing any space in the last
357 * block of the file that is beyond the EOF. We do this since the
358 * size is being increased without writing anything to that block
359 * and we don't want anyone to read the garbage on the disk.
360 */
361 STATIC int /* error (positive) */
362 xfs_zero_last_block(
363 struct inode *ip,
364 xfs_iocore_t *io,
365 xfs_fsize_t isize,
366 xfs_fsize_t end_size)
367 {
368 xfs_fileoff_t last_fsb;
369 xfs_mount_t *mp;
370 int nimaps;
371 int zero_offset;
372 int zero_len;
373 int error = 0;
374 xfs_bmbt_irec_t imap;
375 loff_t loff;
376
377 ASSERT(ismrlocked(io->io_lock, MR_UPDATE) != 0);
378
379 mp = io->io_mount;
380
381 zero_offset = XFS_B_FSB_OFFSET(mp, isize);
382 if (zero_offset == 0) {
383 /*
384 * There are no extra bytes in the last block on disk to
385 * zero, so return.
386 */
387 return 0;
388 }
389
390 last_fsb = XFS_B_TO_FSBT(mp, isize);
391 nimaps = 1;
392 error = XFS_BMAPI(mp, NULL, io, last_fsb, 1, 0, NULL, 0, &imap,
393 &nimaps, NULL);
394 if (error) {
395 return error;
396 }
397 ASSERT(nimaps > 0);
398 /*
399 * If the block underlying isize is just a hole, then there
400 * is nothing to zero.
401 */
402 if (imap.br_startblock == HOLESTARTBLOCK) {
403 return 0;
404 }
405 /*
406 * Zero the part of the last block beyond the EOF, and write it
407 * out sync. We need to drop the ilock while we do this so we
408 * don't deadlock when the buffer cache calls back to us.
409 */
410 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
411 loff = XFS_FSB_TO_B(mp, last_fsb);
412
413 zero_len = mp->m_sb.sb_blocksize - zero_offset;
414
415 error = xfs_iozero(ip, loff + zero_offset, zero_len, end_size);
416
417 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
418 ASSERT(error >= 0);
419 return error;
420 }
421
422 /*
423 * Zero any on disk space between the current EOF and the new,
424 * larger EOF. This handles the normal case of zeroing the remainder
425 * of the last block in the file and the unusual case of zeroing blocks
426 * out beyond the size of the file. This second case only happens
427 * with fixed size extents and when the system crashes before the inode
428 * size was updated but after blocks were allocated. If fill is set,
429 * then any holes in the range are filled and zeroed. If not, the holes
430 * are left alone as holes.
431 */
432
433 int /* error (positive) */
434 xfs_zero_eof(
435 vnode_t *vp,
436 xfs_iocore_t *io,
437 xfs_off_t offset, /* starting I/O offset */
438 xfs_fsize_t isize, /* current inode size */
439 xfs_fsize_t end_size) /* terminal inode size */
440 {
441 struct inode *ip = LINVFS_GET_IP(vp);
442 xfs_fileoff_t start_zero_fsb;
443 xfs_fileoff_t end_zero_fsb;
444 xfs_fileoff_t zero_count_fsb;
445 xfs_fileoff_t last_fsb;
446 xfs_extlen_t buf_len_fsb;
447 xfs_mount_t *mp;
448 int nimaps;
449 int error = 0;
450 xfs_bmbt_irec_t imap;
451
452 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
453 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
454 ASSERT(offset > isize);
455
456 mp = io->io_mount;
457
458 /*
459 * First handle zeroing the block on which isize resides.
460 * We only zero a part of that block so it is handled specially.
461 */
462 error = xfs_zero_last_block(ip, io, isize, end_size);
463 if (error) {
464 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
465 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
466 return error;
467 }
468
469 /*
470 * Calculate the range between the new size and the old
471 * where blocks needing to be zeroed may exist. To get the
472 * block where the last byte in the file currently resides,
473 * we need to subtract one from the size and truncate back
474 * to a block boundary. We subtract 1 in case the size is
475 * exactly on a block boundary.
476 */
477 last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
478 start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
479 end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
480 ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
481 if (last_fsb == end_zero_fsb) {
482 /*
483 * The size was only incremented on its last block.
484 * We took care of that above, so just return.
485 */
486 return 0;
487 }
488
489 ASSERT(start_zero_fsb <= end_zero_fsb);
490 while (start_zero_fsb <= end_zero_fsb) {
491 nimaps = 1;
492 zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
493 error = XFS_BMAPI(mp, NULL, io, start_zero_fsb, zero_count_fsb,
494 0, NULL, 0, &imap, &nimaps, NULL);
495 if (error) {
496 ASSERT(ismrlocked(io->io_lock, MR_UPDATE));
497 ASSERT(ismrlocked(io->io_iolock, MR_UPDATE));
498 return error;
499 }
500 ASSERT(nimaps > 0);
501
502 if (imap.br_state == XFS_EXT_UNWRITTEN ||
503 imap.br_startblock == HOLESTARTBLOCK) {
504 /*
505 * This loop handles initializing pages that were
506 * partially initialized by the code below this
507 * loop. It basically zeroes the part of the page
508 * that sits on a hole and sets the page as P_HOLE
509 * and calls remapf if it is a mapped file.
510 */
511 start_zero_fsb = imap.br_startoff + imap.br_blockcount;
512 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
513 continue;
514 }
515
516 /*
517 * There are blocks in the range requested.
518 * Zero them a single write at a time. We actually
519 * don't zero the entire range returned if it is
520 * too big and simply loop around to get the rest.
521 * That is not the most efficient thing to do, but it
522 * is simple and this path should not be exercised often.
523 */
524 buf_len_fsb = XFS_FILBLKS_MIN(imap.br_blockcount,
525 mp->m_writeio_blocks << 8);
526 /*
527 * Drop the inode lock while we're doing the I/O.
528 * We'll still have the iolock to protect us.
529 */
530 XFS_IUNLOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
531
532 error = xfs_iozero(ip,
533 XFS_FSB_TO_B(mp, start_zero_fsb),
534 XFS_FSB_TO_B(mp, buf_len_fsb),
535 end_size);
536
537 if (error) {
538 goto out_lock;
539 }
540
541 start_zero_fsb = imap.br_startoff + buf_len_fsb;
542 ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
543
544 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
545 }
546
547 return 0;
548
549 out_lock:
550
551 XFS_ILOCK(mp, io, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
552 ASSERT(error >= 0);
553 return error;
554 }
555
556 ssize_t /* bytes written, or (-) error */
557 xfs_write(
558 bhv_desc_t *bdp,
559 struct kiocb *iocb,
560 const struct iovec *iovp,
561 unsigned int nsegs,
562 loff_t *offset,
563 int ioflags,
564 cred_t *credp)
565 {
566 struct file *file = iocb->ki_filp;
567 struct address_space *mapping = file->f_mapping;
568 struct inode *inode = mapping->host;
569 unsigned long segs = nsegs;
570 xfs_inode_t *xip;
571 xfs_mount_t *mp;
572 ssize_t ret = 0, error = 0;
573 xfs_fsize_t isize, new_size;
574 xfs_iocore_t *io;
575 vnode_t *vp;
576 unsigned long seg;
577 int iolock;
578 int eventsent = 0;
579 vrwlock_t locktype;
580 size_t ocount = 0, count;
581 loff_t pos;
582 int need_isem = 1, need_flush = 0;
583
584 XFS_STATS_INC(xs_write_calls);
585
586 vp = BHV_TO_VNODE(bdp);
587 xip = XFS_BHVTOI(bdp);
588
589 for (seg = 0; seg < segs; seg++) {
590 const struct iovec *iv = &iovp[seg];
591
592 /*
593 * If any segment has a negative length, or the cumulative
594 * length ever wraps negative then return -EINVAL.
595 */
596 ocount += iv->iov_len;
597 if (unlikely((ssize_t)(ocount|iv->iov_len) < 0))
598 return -EINVAL;
599 if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len))
600 continue;
601 if (seg == 0)
602 return -EFAULT;
603 segs = seg;
604 ocount -= iv->iov_len; /* This segment is no good */
605 break;
606 }
607
608 count = ocount;
609 pos = *offset;
610
611 if (count == 0)
612 return 0;
613
614 io = &xip->i_iocore;
615 mp = io->io_mount;
616
617 if (XFS_FORCED_SHUTDOWN(mp))
618 return -EIO;
619
620 fs_check_frozen(vp->v_vfsp, SB_FREEZE_WRITE);
621
622 if (ioflags & IO_ISDIRECT) {
623 xfs_buftarg_t *target =
624 (xip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
625 mp->m_rtdev_targp : mp->m_ddev_targp;
626
627 if ((pos & target->pbr_smask) || (count & target->pbr_smask))
628 return XFS_ERROR(-EINVAL);
629
630 if (!VN_CACHED(vp) && pos < i_size_read(inode))
631 need_isem = 0;
632
633 if (VN_CACHED(vp))
634 need_flush = 1;
635 }
636
637 relock:
638 if (need_isem) {
639 iolock = XFS_IOLOCK_EXCL;
640 locktype = VRWLOCK_WRITE;
641
642 mutex_lock(&inode->i_mutex);
643 } else {
644 iolock = XFS_IOLOCK_SHARED;
645 locktype = VRWLOCK_WRITE_DIRECT;
646 }
647
648 xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
649
650 isize = i_size_read(inode);
651
652 if (file->f_flags & O_APPEND)
653 *offset = isize;
654
655 start:
656 error = -generic_write_checks(file, &pos, &count,
657 S_ISBLK(inode->i_mode));
658 if (error) {
659 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
660 goto out_unlock_isem;
661 }
662
663 new_size = pos + count;
664 if (new_size > isize)
665 io->io_new_size = new_size;
666
667 if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
668 !(ioflags & IO_INVIS) && !eventsent)) {
669 loff_t savedsize = pos;
670 int dmflags = FILP_DELAY_FLAG(file);
671
672 if (need_isem)
673 dmflags |= DM_FLAGS_IMUX;
674
675 xfs_iunlock(xip, XFS_ILOCK_EXCL);
676 error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
677 pos, count,
678 dmflags, &locktype);
679 if (error) {
680 xfs_iunlock(xip, iolock);
681 goto out_unlock_isem;
682 }
683 xfs_ilock(xip, XFS_ILOCK_EXCL);
684 eventsent = 1;
685
686 /*
687 * The iolock was dropped and reaquired in XFS_SEND_DATA
688 * so we have to recheck the size when appending.
689 * We will only "goto start;" once, since having sent the
690 * event prevents another call to XFS_SEND_DATA, which is
691 * what allows the size to change in the first place.
692 */
693 if ((file->f_flags & O_APPEND) && savedsize != isize) {
694 pos = isize = xip->i_d.di_size;
695 goto start;
696 }
697 }
698
699 if (likely(!(ioflags & IO_INVIS))) {
700 file_update_time(file);
701 xfs_ichgtime_fast(xip, inode,
702 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
703 }
704
705 /*
706 * If the offset is beyond the size of the file, we have a couple
707 * of things to do. First, if there is already space allocated
708 * we need to either create holes or zero the disk or ...
709 *
710 * If there is a page where the previous size lands, we need
711 * to zero it out up to the new size.
712 */
713
714 if (pos > isize) {
715 error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos,
716 isize, pos + count);
717 if (error) {
718 xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
719 goto out_unlock_isem;
720 }
721 }
722 xfs_iunlock(xip, XFS_ILOCK_EXCL);
723
724 /*
725 * If we're writing the file then make sure to clear the
726 * setuid and setgid bits if the process is not being run
727 * by root. This keeps people from modifying setuid and
728 * setgid binaries.
729 */
730
731 if (((xip->i_d.di_mode & S_ISUID) ||
732 ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
733 (S_ISGID | S_IXGRP))) &&
734 !capable(CAP_FSETID)) {
735 error = xfs_write_clear_setuid(xip);
736 if (likely(!error))
737 error = -remove_suid(file->f_dentry);
738 if (unlikely(error)) {
739 xfs_iunlock(xip, iolock);
740 goto out_unlock_isem;
741 }
742 }
743
744 retry:
745 /* We can write back this queue in page reclaim */
746 current->backing_dev_info = mapping->backing_dev_info;
747
748 if ((ioflags & IO_ISDIRECT)) {
749 if (need_flush) {
750 xfs_inval_cached_trace(io, pos, -1,
751 ctooff(offtoct(pos)), -1);
752 VOP_FLUSHINVAL_PAGES(vp, ctooff(offtoct(pos)),
753 -1, FI_REMAPF_LOCKED);
754 }
755
756 if (need_isem) {
757 /* demote the lock now the cached pages are gone */
758 XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
759 mutex_unlock(&inode->i_mutex);
760
761 iolock = XFS_IOLOCK_SHARED;
762 locktype = VRWLOCK_WRITE_DIRECT;
763 need_isem = 0;
764 }
765
766 xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
767 *offset, ioflags);
768 ret = generic_file_direct_write(iocb, iovp,
769 &segs, pos, offset, count, ocount);
770
771 /*
772 * direct-io write to a hole: fall through to buffered I/O
773 * for completing the rest of the request.
774 */
775 if (ret >= 0 && ret != count) {
776 XFS_STATS_ADD(xs_write_bytes, ret);
777
778 pos += ret;
779 count -= ret;
780
781 need_isem = 1;
782 ioflags &= ~IO_ISDIRECT;
783 xfs_iunlock(xip, iolock);
784 goto relock;
785 }
786 } else {
787 xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
788 *offset, ioflags);
789 ret = generic_file_buffered_write(iocb, iovp, segs,
790 pos, offset, count, ret);
791 }
792
793 current->backing_dev_info = NULL;
794
795 if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
796 ret = wait_on_sync_kiocb(iocb);
797
798 if ((ret == -ENOSPC) &&
799 DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
800 !(ioflags & IO_INVIS)) {
801
802 xfs_rwunlock(bdp, locktype);
803 if (need_isem)
804 mutex_unlock(&inode->i_mutex);
805 error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
806 DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
807 0, 0, 0); /* Delay flag intentionally unused */
808 if (error)
809 goto out_nounlocks;
810 if (need_isem)
811 mutex_lock(&inode->i_mutex);
812 xfs_rwlock(bdp, locktype);
813 pos = xip->i_d.di_size;
814 ret = 0;
815 goto retry;
816 }
817
818 if (*offset > xip->i_d.di_size) {
819 xfs_ilock(xip, XFS_ILOCK_EXCL);
820 if (*offset > xip->i_d.di_size) {
821 xip->i_d.di_size = *offset;
822 i_size_write(inode, *offset);
823 xip->i_update_core = 1;
824 xip->i_update_size = 1;
825 }
826 xfs_iunlock(xip, XFS_ILOCK_EXCL);
827 }
828
829 error = -ret;
830 if (ret <= 0)
831 goto out_unlock_internal;
832
833 XFS_STATS_ADD(xs_write_bytes, ret);
834
835 /* Handle various SYNC-type writes */
836 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
837 /*
838 * If we're treating this as O_DSYNC and we have not updated the
839 * size, force the log.
840 */
841 if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
842 !(xip->i_update_size)) {
843 xfs_inode_log_item_t *iip = xip->i_itemp;
844
845 /*
846 * If an allocation transaction occurred
847 * without extending the size, then we have to force
848 * the log up the proper point to ensure that the
849 * allocation is permanent. We can't count on
850 * the fact that buffered writes lock out direct I/O
851 * writes - the direct I/O write could have extended
852 * the size nontransactionally, then finished before
853 * we started. xfs_write_file will think that the file
854 * didn't grow but the update isn't safe unless the
855 * size change is logged.
856 *
857 * Force the log if we've committed a transaction
858 * against the inode or if someone else has and
859 * the commit record hasn't gone to disk (e.g.
860 * the inode is pinned). This guarantees that
861 * all changes affecting the inode are permanent
862 * when we return.
863 */
864 if (iip && iip->ili_last_lsn) {
865 xfs_log_force(mp, iip->ili_last_lsn,
866 XFS_LOG_FORCE | XFS_LOG_SYNC);
867 } else if (xfs_ipincount(xip) > 0) {
868 xfs_log_force(mp, (xfs_lsn_t)0,
869 XFS_LOG_FORCE | XFS_LOG_SYNC);
870 }
871
872 } else {
873 xfs_trans_t *tp;
874
875 /*
876 * O_SYNC or O_DSYNC _with_ a size update are handled
877 * the same way.
878 *
879 * If the write was synchronous then we need to make
880 * sure that the inode modification time is permanent.
881 * We'll have updated the timestamp above, so here
882 * we use a synchronous transaction to log the inode.
883 * It's not fast, but it's necessary.
884 *
885 * If this a dsync write and the size got changed
886 * non-transactionally, then we need to ensure that
887 * the size change gets logged in a synchronous
888 * transaction.
889 */
890
891 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
892 if ((error = xfs_trans_reserve(tp, 0,
893 XFS_SWRITE_LOG_RES(mp),
894 0, 0, 0))) {
895 /* Transaction reserve failed */
896 xfs_trans_cancel(tp, 0);
897 } else {
898 /* Transaction reserve successful */
899 xfs_ilock(xip, XFS_ILOCK_EXCL);
900 xfs_trans_ijoin(tp, xip, XFS_ILOCK_EXCL);
901 xfs_trans_ihold(tp, xip);
902 xfs_trans_log_inode(tp, xip, XFS_ILOG_CORE);
903 xfs_trans_set_sync(tp);
904 error = xfs_trans_commit(tp, 0, NULL);
905 xfs_iunlock(xip, XFS_ILOCK_EXCL);
906 }
907 if (error)
908 goto out_unlock_internal;
909 }
910
911 xfs_rwunlock(bdp, locktype);
912 if (need_isem)
913 mutex_unlock(&inode->i_mutex);
914
915 error = sync_page_range(inode, mapping, pos, ret);
916 if (!error)
917 error = ret;
918 return error;
919 }
920
921 out_unlock_internal:
922 xfs_rwunlock(bdp, locktype);
923 out_unlock_isem:
924 if (need_isem)
925 mutex_unlock(&inode->i_mutex);
926 out_nounlocks:
927 return -error;
928 }
929
930 /*
931 * All xfs metadata buffers except log state machine buffers
932 * get this attached as their b_bdstrat callback function.
933 * This is so that we can catch a buffer
934 * after prematurely unpinning it to forcibly shutdown the filesystem.
935 */
936 int
937 xfs_bdstrat_cb(struct xfs_buf *bp)
938 {
939 xfs_mount_t *mp;
940
941 mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
942 if (!XFS_FORCED_SHUTDOWN(mp)) {
943 pagebuf_iorequest(bp);
944 return 0;
945 } else {
946 xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
947 /*
948 * Metadata write that didn't get logged but
949 * written delayed anyway. These aren't associated
950 * with a transaction, and can be ignored.
951 */
952 if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
953 (XFS_BUF_ISREAD(bp)) == 0)
954 return (xfs_bioerror_relse(bp));
955 else
956 return (xfs_bioerror(bp));
957 }
958 }
959
960
961 int
962 xfs_bmap(bhv_desc_t *bdp,
963 xfs_off_t offset,
964 ssize_t count,
965 int flags,
966 xfs_iomap_t *iomapp,
967 int *niomaps)
968 {
969 xfs_inode_t *ip = XFS_BHVTOI(bdp);
970 xfs_iocore_t *io = &ip->i_iocore;
971
972 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
973 ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
974 ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
975
976 return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
977 }
978
979 /*
980 * Wrapper around bdstrat so that we can stop data
981 * from going to disk in case we are shutting down the filesystem.
982 * Typically user data goes thru this path; one of the exceptions
983 * is the superblock.
984 */
985 int
986 xfsbdstrat(
987 struct xfs_mount *mp,
988 struct xfs_buf *bp)
989 {
990 ASSERT(mp);
991 if (!XFS_FORCED_SHUTDOWN(mp)) {
992 /* Grio redirection would go here
993 * if (XFS_BUF_IS_GRIO(bp)) {
994 */
995
996 pagebuf_iorequest(bp);
997 return 0;
998 }
999
1000 xfs_buftrace("XFSBDSTRAT IOERROR", bp);
1001 return (xfs_bioerror_relse(bp));
1002 }
1003
1004 /*
1005 * If the underlying (data/log/rt) device is readonly, there are some
1006 * operations that cannot proceed.
1007 */
1008 int
1009 xfs_dev_is_read_only(
1010 xfs_mount_t *mp,
1011 char *message)
1012 {
1013 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1014 xfs_readonly_buftarg(mp->m_logdev_targp) ||
1015 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1016 cmn_err(CE_NOTE,
1017 "XFS: %s required on read-only device.", message);
1018 cmn_err(CE_NOTE,
1019 "XFS: write access unavailable, cannot proceed.");
1020 return EROFS;
1021 }
1022 return 0;
1023 }