xfs_lrw.c 25 KB

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