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