xfs_lrw.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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 == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
  232. ret = wait_on_sync_kiocb(iocb);
  233. if (ret > 0)
  234. XFS_STATS_ADD(xs_read_bytes, ret);
  235. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  236. return ret;
  237. }
  238. ssize_t
  239. xfs_splice_read(
  240. xfs_inode_t *ip,
  241. struct file *infilp,
  242. loff_t *ppos,
  243. struct pipe_inode_info *pipe,
  244. size_t count,
  245. int flags,
  246. int ioflags)
  247. {
  248. xfs_mount_t *mp = ip->i_mount;
  249. ssize_t ret;
  250. XFS_STATS_INC(xs_read_calls);
  251. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  252. return -EIO;
  253. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  254. if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
  255. int iolock = XFS_IOLOCK_SHARED;
  256. int error;
  257. error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, *ppos, count,
  258. FILP_DELAY_FLAG(infilp), &iolock);
  259. if (error) {
  260. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  261. return -error;
  262. }
  263. }
  264. xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, ip,
  265. pipe, count, *ppos, ioflags);
  266. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  267. if (ret > 0)
  268. XFS_STATS_ADD(xs_read_bytes, ret);
  269. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  270. return ret;
  271. }
  272. ssize_t
  273. xfs_splice_write(
  274. xfs_inode_t *ip,
  275. struct pipe_inode_info *pipe,
  276. struct file *outfilp,
  277. loff_t *ppos,
  278. size_t count,
  279. int flags,
  280. int ioflags)
  281. {
  282. xfs_mount_t *mp = ip->i_mount;
  283. ssize_t ret;
  284. struct inode *inode = outfilp->f_mapping->host;
  285. xfs_fsize_t isize, new_size;
  286. XFS_STATS_INC(xs_write_calls);
  287. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  288. return -EIO;
  289. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  290. if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
  291. int iolock = XFS_IOLOCK_EXCL;
  292. int error;
  293. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, *ppos, count,
  294. FILP_DELAY_FLAG(outfilp), &iolock);
  295. if (error) {
  296. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  297. return -error;
  298. }
  299. }
  300. new_size = *ppos + count;
  301. xfs_ilock(ip, XFS_ILOCK_EXCL);
  302. if (new_size > ip->i_size)
  303. ip->i_new_size = new_size;
  304. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  305. xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, ip,
  306. pipe, count, *ppos, ioflags);
  307. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  308. if (ret > 0)
  309. XFS_STATS_ADD(xs_write_bytes, ret);
  310. isize = i_size_read(inode);
  311. if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
  312. *ppos = isize;
  313. if (*ppos > ip->i_size) {
  314. xfs_ilock(ip, XFS_ILOCK_EXCL);
  315. if (*ppos > ip->i_size)
  316. ip->i_size = *ppos;
  317. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  318. }
  319. if (ip->i_new_size) {
  320. xfs_ilock(ip, XFS_ILOCK_EXCL);
  321. ip->i_new_size = 0;
  322. if (ip->i_d.di_size > ip->i_size)
  323. ip->i_d.di_size = ip->i_size;
  324. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  325. }
  326. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  327. return ret;
  328. }
  329. /*
  330. * This routine is called to handle zeroing any space in the last
  331. * block of the file that is beyond the EOF. We do this since the
  332. * size is being increased without writing anything to that block
  333. * and we don't want anyone to read the garbage on the disk.
  334. */
  335. STATIC int /* error (positive) */
  336. xfs_zero_last_block(
  337. xfs_inode_t *ip,
  338. xfs_fsize_t offset,
  339. xfs_fsize_t isize)
  340. {
  341. xfs_fileoff_t last_fsb;
  342. xfs_mount_t *mp = ip->i_mount;
  343. int nimaps;
  344. int zero_offset;
  345. int zero_len;
  346. int error = 0;
  347. xfs_bmbt_irec_t imap;
  348. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  349. zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  350. if (zero_offset == 0) {
  351. /*
  352. * There are no extra bytes in the last block on disk to
  353. * zero, so return.
  354. */
  355. return 0;
  356. }
  357. last_fsb = XFS_B_TO_FSBT(mp, isize);
  358. nimaps = 1;
  359. error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
  360. &nimaps, NULL, NULL);
  361. if (error) {
  362. return error;
  363. }
  364. ASSERT(nimaps > 0);
  365. /*
  366. * If the block underlying isize is just a hole, then there
  367. * is nothing to zero.
  368. */
  369. if (imap.br_startblock == HOLESTARTBLOCK) {
  370. return 0;
  371. }
  372. /*
  373. * Zero the part of the last block beyond the EOF, and write it
  374. * out sync. We need to drop the ilock while we do this so we
  375. * don't deadlock when the buffer cache calls back to us.
  376. */
  377. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  378. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  379. if (isize + zero_len > offset)
  380. zero_len = offset - isize;
  381. error = xfs_iozero(ip, isize, zero_len);
  382. xfs_ilock(ip, XFS_ILOCK_EXCL);
  383. ASSERT(error >= 0);
  384. return error;
  385. }
  386. /*
  387. * Zero any on disk space between the current EOF and the new,
  388. * larger EOF. This handles the normal case of zeroing the remainder
  389. * of the last block in the file and the unusual case of zeroing blocks
  390. * out beyond the size of the file. This second case only happens
  391. * with fixed size extents and when the system crashes before the inode
  392. * size was updated but after blocks were allocated. If fill is set,
  393. * then any holes in the range are filled and zeroed. If not, the holes
  394. * are left alone as holes.
  395. */
  396. int /* error (positive) */
  397. xfs_zero_eof(
  398. xfs_inode_t *ip,
  399. xfs_off_t offset, /* starting I/O offset */
  400. xfs_fsize_t isize) /* current inode size */
  401. {
  402. xfs_mount_t *mp = ip->i_mount;
  403. xfs_fileoff_t start_zero_fsb;
  404. xfs_fileoff_t end_zero_fsb;
  405. xfs_fileoff_t zero_count_fsb;
  406. xfs_fileoff_t last_fsb;
  407. xfs_fileoff_t zero_off;
  408. xfs_fsize_t zero_len;
  409. int nimaps;
  410. int error = 0;
  411. xfs_bmbt_irec_t imap;
  412. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  413. ASSERT(offset > isize);
  414. /*
  415. * First handle zeroing the block on which isize resides.
  416. * We only zero a part of that block so it is handled specially.
  417. */
  418. error = xfs_zero_last_block(ip, offset, isize);
  419. if (error) {
  420. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  421. return error;
  422. }
  423. /*
  424. * Calculate the range between the new size and the old
  425. * where blocks needing to be zeroed may exist. To get the
  426. * block where the last byte in the file currently resides,
  427. * we need to subtract one from the size and truncate back
  428. * to a block boundary. We subtract 1 in case the size is
  429. * exactly on a block boundary.
  430. */
  431. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  432. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  433. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  434. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  435. if (last_fsb == end_zero_fsb) {
  436. /*
  437. * The size was only incremented on its last block.
  438. * We took care of that above, so just return.
  439. */
  440. return 0;
  441. }
  442. ASSERT(start_zero_fsb <= end_zero_fsb);
  443. while (start_zero_fsb <= end_zero_fsb) {
  444. nimaps = 1;
  445. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  446. error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
  447. 0, NULL, 0, &imap, &nimaps, NULL, NULL);
  448. if (error) {
  449. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  450. return error;
  451. }
  452. ASSERT(nimaps > 0);
  453. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  454. imap.br_startblock == HOLESTARTBLOCK) {
  455. /*
  456. * This loop handles initializing pages that were
  457. * partially initialized by the code below this
  458. * loop. It basically zeroes the part of the page
  459. * that sits on a hole and sets the page as P_HOLE
  460. * and calls remapf if it is a mapped file.
  461. */
  462. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  463. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  464. continue;
  465. }
  466. /*
  467. * There are blocks we need to zero.
  468. * Drop the inode lock while we're doing the I/O.
  469. * We'll still have the iolock to protect us.
  470. */
  471. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  472. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  473. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  474. if ((zero_off + zero_len) > offset)
  475. zero_len = offset - zero_off;
  476. error = xfs_iozero(ip, zero_off, zero_len);
  477. if (error) {
  478. goto out_lock;
  479. }
  480. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  481. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  482. xfs_ilock(ip, XFS_ILOCK_EXCL);
  483. }
  484. return 0;
  485. out_lock:
  486. xfs_ilock(ip, XFS_ILOCK_EXCL);
  487. ASSERT(error >= 0);
  488. return error;
  489. }
  490. ssize_t /* bytes written, or (-) error */
  491. xfs_write(
  492. struct xfs_inode *xip,
  493. struct kiocb *iocb,
  494. const struct iovec *iovp,
  495. unsigned int nsegs,
  496. loff_t *offset,
  497. int ioflags)
  498. {
  499. struct file *file = iocb->ki_filp;
  500. struct address_space *mapping = file->f_mapping;
  501. struct inode *inode = mapping->host;
  502. unsigned long segs = nsegs;
  503. xfs_mount_t *mp;
  504. ssize_t ret = 0, error = 0;
  505. xfs_fsize_t isize, new_size;
  506. int iolock;
  507. int eventsent = 0;
  508. size_t ocount = 0, count;
  509. loff_t pos;
  510. int need_i_mutex;
  511. XFS_STATS_INC(xs_write_calls);
  512. error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
  513. if (error)
  514. return error;
  515. count = ocount;
  516. pos = *offset;
  517. if (count == 0)
  518. return 0;
  519. mp = xip->i_mount;
  520. xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
  521. if (XFS_FORCED_SHUTDOWN(mp))
  522. return -EIO;
  523. relock:
  524. if (ioflags & IO_ISDIRECT) {
  525. iolock = XFS_IOLOCK_SHARED;
  526. need_i_mutex = 0;
  527. } else {
  528. iolock = XFS_IOLOCK_EXCL;
  529. need_i_mutex = 1;
  530. mutex_lock(&inode->i_mutex);
  531. }
  532. xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
  533. start:
  534. error = -generic_write_checks(file, &pos, &count,
  535. S_ISBLK(inode->i_mode));
  536. if (error) {
  537. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  538. goto out_unlock_mutex;
  539. }
  540. if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
  541. !(ioflags & IO_INVIS) && !eventsent)) {
  542. int dmflags = FILP_DELAY_FLAG(file);
  543. if (need_i_mutex)
  544. dmflags |= DM_FLAGS_IMUX;
  545. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  546. error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, xip,
  547. pos, count, dmflags, &iolock);
  548. if (error) {
  549. goto out_unlock_internal;
  550. }
  551. xfs_ilock(xip, XFS_ILOCK_EXCL);
  552. eventsent = 1;
  553. /*
  554. * The iolock was dropped and reacquired in XFS_SEND_DATA
  555. * so we have to recheck the size when appending.
  556. * We will only "goto start;" once, since having sent the
  557. * event prevents another call to XFS_SEND_DATA, which is
  558. * what allows the size to change in the first place.
  559. */
  560. if ((file->f_flags & O_APPEND) && pos != xip->i_size)
  561. goto start;
  562. }
  563. if (ioflags & IO_ISDIRECT) {
  564. xfs_buftarg_t *target =
  565. XFS_IS_REALTIME_INODE(xip) ?
  566. mp->m_rtdev_targp : mp->m_ddev_targp;
  567. if ((pos & target->bt_smask) || (count & target->bt_smask)) {
  568. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  569. return XFS_ERROR(-EINVAL);
  570. }
  571. if (!need_i_mutex && (mapping->nrpages || pos > xip->i_size)) {
  572. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  573. iolock = XFS_IOLOCK_EXCL;
  574. need_i_mutex = 1;
  575. mutex_lock(&inode->i_mutex);
  576. xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
  577. goto start;
  578. }
  579. }
  580. new_size = pos + count;
  581. if (new_size > xip->i_size)
  582. xip->i_new_size = new_size;
  583. if (likely(!(ioflags & IO_INVIS)))
  584. xfs_ichgtime(xip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  585. /*
  586. * If the offset is beyond the size of the file, we have a couple
  587. * of things to do. First, if there is already space allocated
  588. * we need to either create holes or zero the disk or ...
  589. *
  590. * If there is a page where the previous size lands, we need
  591. * to zero it out up to the new size.
  592. */
  593. if (pos > xip->i_size) {
  594. error = xfs_zero_eof(xip, pos, xip->i_size);
  595. if (error) {
  596. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  597. goto out_unlock_internal;
  598. }
  599. }
  600. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  601. /*
  602. * If we're writing the file then make sure to clear the
  603. * setuid and setgid bits if the process is not being run
  604. * by root. This keeps people from modifying setuid and
  605. * setgid binaries.
  606. */
  607. if (((xip->i_d.di_mode & S_ISUID) ||
  608. ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
  609. (S_ISGID | S_IXGRP))) &&
  610. !capable(CAP_FSETID)) {
  611. error = xfs_write_clear_setuid(xip);
  612. if (likely(!error))
  613. error = -file_remove_suid(file);
  614. if (unlikely(error)) {
  615. goto out_unlock_internal;
  616. }
  617. }
  618. /* We can write back this queue in page reclaim */
  619. current->backing_dev_info = mapping->backing_dev_info;
  620. if ((ioflags & IO_ISDIRECT)) {
  621. if (mapping->nrpages) {
  622. WARN_ON(need_i_mutex == 0);
  623. xfs_inval_cached_trace(xip, pos, -1,
  624. (pos & PAGE_CACHE_MASK), -1);
  625. error = xfs_flushinval_pages(xip,
  626. (pos & PAGE_CACHE_MASK),
  627. -1, FI_REMAPF_LOCKED);
  628. if (error)
  629. goto out_unlock_internal;
  630. }
  631. if (need_i_mutex) {
  632. /* demote the lock now the cached pages are gone */
  633. xfs_ilock_demote(xip, XFS_IOLOCK_EXCL);
  634. mutex_unlock(&inode->i_mutex);
  635. iolock = XFS_IOLOCK_SHARED;
  636. need_i_mutex = 0;
  637. }
  638. xfs_rw_enter_trace(XFS_DIOWR_ENTER, xip, (void *)iovp, segs,
  639. *offset, ioflags);
  640. ret = generic_file_direct_write(iocb, iovp,
  641. &segs, pos, offset, count, ocount);
  642. /*
  643. * direct-io write to a hole: fall through to buffered I/O
  644. * for completing the rest of the request.
  645. */
  646. if (ret >= 0 && ret != count) {
  647. XFS_STATS_ADD(xs_write_bytes, ret);
  648. pos += ret;
  649. count -= ret;
  650. ioflags &= ~IO_ISDIRECT;
  651. xfs_iunlock(xip, iolock);
  652. goto relock;
  653. }
  654. } else {
  655. int enospc = 0;
  656. ssize_t ret2 = 0;
  657. write_retry:
  658. xfs_rw_enter_trace(XFS_WRITE_ENTER, xip, (void *)iovp, segs,
  659. *offset, ioflags);
  660. ret2 = generic_file_buffered_write(iocb, iovp, segs,
  661. pos, offset, count, ret);
  662. /*
  663. * if we just got an ENOSPC, flush the inode now we
  664. * aren't holding any page locks and retry *once*
  665. */
  666. if (ret2 == -ENOSPC && !enospc) {
  667. error = xfs_flush_pages(xip, 0, -1, 0, FI_NONE);
  668. if (error)
  669. goto out_unlock_internal;
  670. enospc = 1;
  671. goto write_retry;
  672. }
  673. ret = ret2;
  674. }
  675. current->backing_dev_info = NULL;
  676. if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
  677. ret = wait_on_sync_kiocb(iocb);
  678. isize = i_size_read(inode);
  679. if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
  680. *offset = isize;
  681. if (*offset > xip->i_size) {
  682. xfs_ilock(xip, XFS_ILOCK_EXCL);
  683. if (*offset > xip->i_size)
  684. xip->i_size = *offset;
  685. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  686. }
  687. if (ret == -ENOSPC &&
  688. DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
  689. xfs_iunlock(xip, iolock);
  690. if (need_i_mutex)
  691. mutex_unlock(&inode->i_mutex);
  692. error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, xip,
  693. DM_RIGHT_NULL, xip, DM_RIGHT_NULL, NULL, NULL,
  694. 0, 0, 0); /* Delay flag intentionally unused */
  695. if (need_i_mutex)
  696. mutex_lock(&inode->i_mutex);
  697. xfs_ilock(xip, iolock);
  698. if (error)
  699. goto out_unlock_internal;
  700. goto start;
  701. }
  702. error = -ret;
  703. if (ret <= 0)
  704. goto out_unlock_internal;
  705. XFS_STATS_ADD(xs_write_bytes, ret);
  706. /* Handle various SYNC-type writes */
  707. if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
  708. int error2;
  709. xfs_iunlock(xip, iolock);
  710. if (need_i_mutex)
  711. mutex_unlock(&inode->i_mutex);
  712. error2 = sync_page_range(inode, mapping, pos, ret);
  713. if (!error)
  714. error = error2;
  715. if (need_i_mutex)
  716. mutex_lock(&inode->i_mutex);
  717. xfs_ilock(xip, iolock);
  718. error2 = xfs_write_sync_logforce(mp, xip);
  719. if (!error)
  720. error = error2;
  721. }
  722. out_unlock_internal:
  723. if (xip->i_new_size) {
  724. xfs_ilock(xip, XFS_ILOCK_EXCL);
  725. xip->i_new_size = 0;
  726. /*
  727. * If this was a direct or synchronous I/O that failed (such
  728. * as ENOSPC) then part of the I/O may have been written to
  729. * disk before the error occured. In this case the on-disk
  730. * file size may have been adjusted beyond the in-memory file
  731. * size and now needs to be truncated back.
  732. */
  733. if (xip->i_d.di_size > xip->i_size)
  734. xip->i_d.di_size = xip->i_size;
  735. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  736. }
  737. xfs_iunlock(xip, iolock);
  738. out_unlock_mutex:
  739. if (need_i_mutex)
  740. mutex_unlock(&inode->i_mutex);
  741. return -error;
  742. }
  743. /*
  744. * All xfs metadata buffers except log state machine buffers
  745. * get this attached as their b_bdstrat callback function.
  746. * This is so that we can catch a buffer
  747. * after prematurely unpinning it to forcibly shutdown the filesystem.
  748. */
  749. int
  750. xfs_bdstrat_cb(struct xfs_buf *bp)
  751. {
  752. if (XFS_FORCED_SHUTDOWN(bp->b_mount)) {
  753. xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
  754. /*
  755. * Metadata write that didn't get logged but
  756. * written delayed anyway. These aren't associated
  757. * with a transaction, and can be ignored.
  758. */
  759. if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
  760. (XFS_BUF_ISREAD(bp)) == 0)
  761. return (xfs_bioerror_relse(bp));
  762. else
  763. return (xfs_bioerror(bp));
  764. }
  765. xfs_buf_iorequest(bp);
  766. return 0;
  767. }
  768. /*
  769. * Wrapper around bdstrat so that we can stop data from going to disk in case
  770. * we are shutting down the filesystem. Typically user data goes thru this
  771. * path; one of the exceptions is the superblock.
  772. */
  773. void
  774. xfsbdstrat(
  775. struct xfs_mount *mp,
  776. struct xfs_buf *bp)
  777. {
  778. ASSERT(mp);
  779. if (!XFS_FORCED_SHUTDOWN(mp)) {
  780. xfs_buf_iorequest(bp);
  781. return;
  782. }
  783. xfs_buftrace("XFSBDSTRAT IOERROR", bp);
  784. xfs_bioerror_relse(bp);
  785. }
  786. /*
  787. * If the underlying (data/log/rt) device is readonly, there are some
  788. * operations that cannot proceed.
  789. */
  790. int
  791. xfs_dev_is_read_only(
  792. xfs_mount_t *mp,
  793. char *message)
  794. {
  795. if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
  796. xfs_readonly_buftarg(mp->m_logdev_targp) ||
  797. (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
  798. cmn_err(CE_NOTE,
  799. "XFS: %s required on read-only device.", message);
  800. cmn_err(CE_NOTE,
  801. "XFS: write access unavailable, cannot proceed.");
  802. return EROFS;
  803. }
  804. return 0;
  805. }