xfs_lrw.c 26 KB

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