xfs_lrw.c 23 KB

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