xfs_lrw.c 25 KB

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