xfs_lrw.c 23 KB

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