xfs_lrw.c 24 KB

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