xfs_lrw.c 26 KB

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