xfs_lrw.c 27 KB

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