xfs_lrw.c 25 KB

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