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