xfs_lrw.c 26 KB

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