xfs_lrw.c 26 KB

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