xfs_lrw.c 25 KB

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