xfs_lrw.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_bmap.h"
  39. #include "xfs_btree.h"
  40. #include "xfs_ialloc.h"
  41. #include "xfs_rtalloc.h"
  42. #include "xfs_error.h"
  43. #include "xfs_itable.h"
  44. #include "xfs_rw.h"
  45. #include "xfs_acl.h"
  46. #include "xfs_attr.h"
  47. #include "xfs_inode_item.h"
  48. #include "xfs_buf_item.h"
  49. #include "xfs_utils.h"
  50. #include "xfs_iomap.h"
  51. #include "xfs_vnodeops.h"
  52. #include <linux/capability.h>
  53. #include <linux/writeback.h>
  54. #if defined(XFS_RW_TRACE)
  55. void
  56. xfs_rw_enter_trace(
  57. int tag,
  58. xfs_inode_t *ip,
  59. void *data,
  60. size_t segs,
  61. loff_t offset,
  62. int ioflags)
  63. {
  64. if (ip->i_rwtrace == NULL)
  65. return;
  66. ktrace_enter(ip->i_rwtrace,
  67. (void *)(unsigned long)tag,
  68. (void *)ip,
  69. (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
  70. (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
  71. (void *)data,
  72. (void *)((unsigned long)segs),
  73. (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
  74. (void *)((unsigned long)(offset & 0xffffffff)),
  75. (void *)((unsigned long)ioflags),
  76. (void *)((unsigned long)((ip->i_new_size >> 32) & 0xffffffff)),
  77. (void *)((unsigned long)(ip->i_new_size & 0xffffffff)),
  78. (void *)((unsigned long)current_pid()),
  79. (void *)NULL,
  80. (void *)NULL,
  81. (void *)NULL,
  82. (void *)NULL);
  83. }
  84. void
  85. xfs_inval_cached_trace(
  86. xfs_inode_t *ip,
  87. xfs_off_t offset,
  88. xfs_off_t len,
  89. xfs_off_t first,
  90. xfs_off_t last)
  91. {
  92. if (ip->i_rwtrace == NULL)
  93. return;
  94. ktrace_enter(ip->i_rwtrace,
  95. (void *)(__psint_t)XFS_INVAL_CACHED,
  96. (void *)ip,
  97. (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
  98. (void *)((unsigned long)(offset & 0xffffffff)),
  99. (void *)((unsigned long)((len >> 32) & 0xffffffff)),
  100. (void *)((unsigned long)(len & 0xffffffff)),
  101. (void *)((unsigned long)((first >> 32) & 0xffffffff)),
  102. (void *)((unsigned long)(first & 0xffffffff)),
  103. (void *)((unsigned long)((last >> 32) & 0xffffffff)),
  104. (void *)((unsigned long)(last & 0xffffffff)),
  105. (void *)((unsigned long)current_pid()),
  106. (void *)NULL,
  107. (void *)NULL,
  108. (void *)NULL,
  109. (void *)NULL,
  110. (void *)NULL);
  111. }
  112. #endif
  113. /*
  114. * xfs_iozero
  115. *
  116. * xfs_iozero clears the specified range of buffer supplied,
  117. * and marks all the affected blocks as valid and modified. If
  118. * an affected block is not allocated, it will be allocated. If
  119. * an affected block is not completely overwritten, and is not
  120. * valid before the operation, it will be read from disk before
  121. * being partially zeroed.
  122. */
  123. STATIC int
  124. xfs_iozero(
  125. struct xfs_inode *ip, /* inode */
  126. loff_t pos, /* offset in file */
  127. size_t count) /* size of data to zero */
  128. {
  129. struct page *page;
  130. struct address_space *mapping;
  131. int status;
  132. mapping = ip->i_vnode->i_mapping;
  133. do {
  134. unsigned offset, bytes;
  135. void *fsdata;
  136. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  137. bytes = PAGE_CACHE_SIZE - offset;
  138. if (bytes > count)
  139. bytes = count;
  140. status = pagecache_write_begin(NULL, mapping, pos, bytes,
  141. AOP_FLAG_UNINTERRUPTIBLE,
  142. &page, &fsdata);
  143. if (status)
  144. break;
  145. zero_user(page, offset, bytes);
  146. status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
  147. page, fsdata);
  148. WARN_ON(status <= 0); /* can't return less than zero! */
  149. pos += bytes;
  150. count -= bytes;
  151. status = 0;
  152. } while (count);
  153. return (-status);
  154. }
  155. ssize_t /* bytes read, or (-) error */
  156. xfs_read(
  157. xfs_inode_t *ip,
  158. struct kiocb *iocb,
  159. const struct iovec *iovp,
  160. unsigned int segs,
  161. loff_t *offset,
  162. int ioflags)
  163. {
  164. struct file *file = iocb->ki_filp;
  165. struct inode *inode = file->f_mapping->host;
  166. bhv_vnode_t *vp = XFS_ITOV(ip);
  167. xfs_mount_t *mp = ip->i_mount;
  168. size_t size = 0;
  169. ssize_t ret = 0;
  170. xfs_fsize_t n;
  171. unsigned long seg;
  172. XFS_STATS_INC(xs_read_calls);
  173. /* START copy & waste from filemap.c */
  174. for (seg = 0; seg < segs; seg++) {
  175. const struct iovec *iv = &iovp[seg];
  176. /*
  177. * If any segment has a negative length, or the cumulative
  178. * length ever wraps negative then return -EINVAL.
  179. */
  180. size += iv->iov_len;
  181. if (unlikely((ssize_t)(size|iv->iov_len) < 0))
  182. return XFS_ERROR(-EINVAL);
  183. }
  184. /* END copy & waste from filemap.c */
  185. if (unlikely(ioflags & IO_ISDIRECT)) {
  186. xfs_buftarg_t *target =
  187. XFS_IS_REALTIME_INODE(ip) ?
  188. mp->m_rtdev_targp : mp->m_ddev_targp;
  189. if ((*offset & target->bt_smask) ||
  190. (size & target->bt_smask)) {
  191. if (*offset == ip->i_size) {
  192. return (0);
  193. }
  194. return -XFS_ERROR(EINVAL);
  195. }
  196. }
  197. n = XFS_MAXIOFFSET(mp) - *offset;
  198. if ((n <= 0) || (size == 0))
  199. return 0;
  200. if (n < size)
  201. size = n;
  202. if (XFS_FORCED_SHUTDOWN(mp))
  203. return -EIO;
  204. if (unlikely(ioflags & IO_ISDIRECT))
  205. mutex_lock(&inode->i_mutex);
  206. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  207. if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
  208. bhv_vrwlock_t locktype = VRWLOCK_READ;
  209. int dmflags = FILP_DELAY_FLAG(file) | DM_SEM_FLAG_RD(ioflags);
  210. ret = -XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *offset, size,
  211. dmflags, &locktype);
  212. if (ret) {
  213. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  214. if (unlikely(ioflags & IO_ISDIRECT))
  215. mutex_unlock(&inode->i_mutex);
  216. return ret;
  217. }
  218. }
  219. if (unlikely(ioflags & IO_ISDIRECT)) {
  220. if (VN_CACHED(vp))
  221. ret = xfs_flushinval_pages(ip, (*offset & PAGE_CACHE_MASK),
  222. -1, FI_REMAPF_LOCKED);
  223. mutex_unlock(&inode->i_mutex);
  224. if (ret) {
  225. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  226. return ret;
  227. }
  228. }
  229. xfs_rw_enter_trace(XFS_READ_ENTER, ip,
  230. (void *)iovp, segs, *offset, ioflags);
  231. iocb->ki_pos = *offset;
  232. ret = generic_file_aio_read(iocb, iovp, segs, *offset);
  233. if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
  234. ret = wait_on_sync_kiocb(iocb);
  235. if (ret > 0)
  236. XFS_STATS_ADD(xs_read_bytes, ret);
  237. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  238. return ret;
  239. }
  240. ssize_t
  241. xfs_splice_read(
  242. xfs_inode_t *ip,
  243. struct file *infilp,
  244. loff_t *ppos,
  245. struct pipe_inode_info *pipe,
  246. size_t count,
  247. int flags,
  248. int ioflags)
  249. {
  250. bhv_vnode_t *vp = XFS_ITOV(ip);
  251. xfs_mount_t *mp = ip->i_mount;
  252. ssize_t ret;
  253. XFS_STATS_INC(xs_read_calls);
  254. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  255. return -EIO;
  256. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  257. if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && !(ioflags & IO_INVIS)) {
  258. bhv_vrwlock_t locktype = VRWLOCK_READ;
  259. int error;
  260. error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, *ppos, count,
  261. FILP_DELAY_FLAG(infilp), &locktype);
  262. if (error) {
  263. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  264. return -error;
  265. }
  266. }
  267. xfs_rw_enter_trace(XFS_SPLICE_READ_ENTER, ip,
  268. pipe, count, *ppos, ioflags);
  269. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  270. if (ret > 0)
  271. XFS_STATS_ADD(xs_read_bytes, ret);
  272. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  273. return ret;
  274. }
  275. ssize_t
  276. xfs_splice_write(
  277. xfs_inode_t *ip,
  278. struct pipe_inode_info *pipe,
  279. struct file *outfilp,
  280. loff_t *ppos,
  281. size_t count,
  282. int flags,
  283. int ioflags)
  284. {
  285. bhv_vnode_t *vp = XFS_ITOV(ip);
  286. xfs_mount_t *mp = ip->i_mount;
  287. ssize_t ret;
  288. struct inode *inode = outfilp->f_mapping->host;
  289. xfs_fsize_t isize, new_size;
  290. XFS_STATS_INC(xs_write_calls);
  291. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  292. return -EIO;
  293. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  294. if (DM_EVENT_ENABLED(ip, DM_EVENT_WRITE) && !(ioflags & IO_INVIS)) {
  295. bhv_vrwlock_t locktype = VRWLOCK_WRITE;
  296. int error;
  297. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp, *ppos, count,
  298. FILP_DELAY_FLAG(outfilp), &locktype);
  299. if (error) {
  300. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  301. return -error;
  302. }
  303. }
  304. new_size = *ppos + count;
  305. xfs_ilock(ip, XFS_ILOCK_EXCL);
  306. if (new_size > ip->i_size)
  307. ip->i_new_size = new_size;
  308. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  309. xfs_rw_enter_trace(XFS_SPLICE_WRITE_ENTER, ip,
  310. pipe, count, *ppos, ioflags);
  311. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  312. if (ret > 0)
  313. XFS_STATS_ADD(xs_write_bytes, ret);
  314. isize = i_size_read(inode);
  315. if (unlikely(ret < 0 && ret != -EFAULT && *ppos > isize))
  316. *ppos = isize;
  317. if (*ppos > ip->i_size) {
  318. xfs_ilock(ip, XFS_ILOCK_EXCL);
  319. if (*ppos > ip->i_size)
  320. ip->i_size = *ppos;
  321. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  322. }
  323. if (ip->i_new_size) {
  324. xfs_ilock(ip, XFS_ILOCK_EXCL);
  325. ip->i_new_size = 0;
  326. if (ip->i_d.di_size > ip->i_size)
  327. ip->i_d.di_size = ip->i_size;
  328. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  329. }
  330. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  331. return ret;
  332. }
  333. /*
  334. * This routine is called to handle zeroing any space in the last
  335. * block of the file that is beyond the EOF. We do this since the
  336. * size is being increased without writing anything to that block
  337. * and we don't want anyone to read the garbage on the disk.
  338. */
  339. STATIC int /* error (positive) */
  340. xfs_zero_last_block(
  341. xfs_inode_t *ip,
  342. xfs_fsize_t offset,
  343. xfs_fsize_t isize)
  344. {
  345. xfs_fileoff_t last_fsb;
  346. xfs_mount_t *mp = ip->i_mount;
  347. int nimaps;
  348. int zero_offset;
  349. int zero_len;
  350. int error = 0;
  351. xfs_bmbt_irec_t imap;
  352. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0);
  353. zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  354. if (zero_offset == 0) {
  355. /*
  356. * There are no extra bytes in the last block on disk to
  357. * zero, so return.
  358. */
  359. return 0;
  360. }
  361. last_fsb = XFS_B_TO_FSBT(mp, isize);
  362. nimaps = 1;
  363. error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
  364. &nimaps, NULL, NULL);
  365. if (error) {
  366. return error;
  367. }
  368. ASSERT(nimaps > 0);
  369. /*
  370. * If the block underlying isize is just a hole, then there
  371. * is nothing to zero.
  372. */
  373. if (imap.br_startblock == HOLESTARTBLOCK) {
  374. return 0;
  375. }
  376. /*
  377. * Zero the part of the last block beyond the EOF, and write it
  378. * out sync. We need to drop the ilock while we do this so we
  379. * don't deadlock when the buffer cache calls back to us.
  380. */
  381. xfs_iunlock(ip, XFS_ILOCK_EXCL| XFS_EXTSIZE_RD);
  382. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  383. if (isize + zero_len > offset)
  384. zero_len = offset - isize;
  385. error = xfs_iozero(ip, isize, zero_len);
  386. xfs_ilock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
  387. ASSERT(error >= 0);
  388. return error;
  389. }
  390. /*
  391. * Zero any on disk space between the current EOF and the new,
  392. * larger EOF. This handles the normal case of zeroing the remainder
  393. * of the last block in the file and the unusual case of zeroing blocks
  394. * out beyond the size of the file. This second case only happens
  395. * with fixed size extents and when the system crashes before the inode
  396. * size was updated but after blocks were allocated. If fill is set,
  397. * then any holes in the range are filled and zeroed. If not, the holes
  398. * are left alone as holes.
  399. */
  400. int /* error (positive) */
  401. xfs_zero_eof(
  402. xfs_inode_t *ip,
  403. xfs_off_t offset, /* starting I/O offset */
  404. xfs_fsize_t isize) /* current inode size */
  405. {
  406. xfs_mount_t *mp = ip->i_mount;
  407. xfs_fileoff_t start_zero_fsb;
  408. xfs_fileoff_t end_zero_fsb;
  409. xfs_fileoff_t zero_count_fsb;
  410. xfs_fileoff_t last_fsb;
  411. xfs_fileoff_t zero_off;
  412. xfs_fsize_t zero_len;
  413. int nimaps;
  414. int error = 0;
  415. xfs_bmbt_irec_t imap;
  416. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  417. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  418. ASSERT(offset > isize);
  419. /*
  420. * First handle zeroing the block on which isize resides.
  421. * We only zero a part of that block so it is handled specially.
  422. */
  423. error = xfs_zero_last_block(ip, offset, isize);
  424. if (error) {
  425. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  426. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  427. return error;
  428. }
  429. /*
  430. * Calculate the range between the new size and the old
  431. * where blocks needing to be zeroed may exist. To get the
  432. * block where the last byte in the file currently resides,
  433. * we need to subtract one from the size and truncate back
  434. * to a block boundary. We subtract 1 in case the size is
  435. * exactly on a block boundary.
  436. */
  437. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  438. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  439. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  440. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  441. if (last_fsb == end_zero_fsb) {
  442. /*
  443. * The size was only incremented on its last block.
  444. * We took care of that above, so just return.
  445. */
  446. return 0;
  447. }
  448. ASSERT(start_zero_fsb <= end_zero_fsb);
  449. while (start_zero_fsb <= end_zero_fsb) {
  450. nimaps = 1;
  451. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  452. error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
  453. 0, NULL, 0, &imap, &nimaps, NULL, NULL);
  454. if (error) {
  455. ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE));
  456. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  457. return error;
  458. }
  459. ASSERT(nimaps > 0);
  460. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  461. imap.br_startblock == HOLESTARTBLOCK) {
  462. /*
  463. * This loop handles initializing pages that were
  464. * partially initialized by the code below this
  465. * loop. It basically zeroes the part of the page
  466. * that sits on a hole and sets the page as P_HOLE
  467. * and calls remapf if it is a mapped file.
  468. */
  469. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  470. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  471. continue;
  472. }
  473. /*
  474. * There are blocks we need to zero.
  475. * Drop the inode lock while we're doing the I/O.
  476. * We'll still have the iolock to protect us.
  477. */
  478. xfs_iunlock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
  479. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  480. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  481. if ((zero_off + zero_len) > offset)
  482. zero_len = offset - zero_off;
  483. error = xfs_iozero(ip, zero_off, zero_len);
  484. if (error) {
  485. goto out_lock;
  486. }
  487. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  488. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  489. xfs_ilock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
  490. }
  491. return 0;
  492. out_lock:
  493. xfs_ilock(ip, XFS_ILOCK_EXCL|XFS_EXTSIZE_RD);
  494. ASSERT(error >= 0);
  495. return error;
  496. }
  497. ssize_t /* bytes written, or (-) error */
  498. xfs_write(
  499. struct xfs_inode *xip,
  500. struct kiocb *iocb,
  501. const struct iovec *iovp,
  502. unsigned int nsegs,
  503. loff_t *offset,
  504. int ioflags)
  505. {
  506. struct file *file = iocb->ki_filp;
  507. struct address_space *mapping = file->f_mapping;
  508. struct inode *inode = mapping->host;
  509. bhv_vnode_t *vp = XFS_ITOV(xip);
  510. unsigned long segs = nsegs;
  511. xfs_mount_t *mp;
  512. ssize_t ret = 0, error = 0;
  513. xfs_fsize_t isize, new_size;
  514. int iolock;
  515. int eventsent = 0;
  516. bhv_vrwlock_t locktype;
  517. size_t ocount = 0, count;
  518. loff_t pos;
  519. int need_i_mutex;
  520. XFS_STATS_INC(xs_write_calls);
  521. error = generic_segment_checks(iovp, &segs, &ocount, VERIFY_READ);
  522. if (error)
  523. return error;
  524. count = ocount;
  525. pos = *offset;
  526. if (count == 0)
  527. return 0;
  528. mp = xip->i_mount;
  529. xfs_wait_for_freeze(mp, SB_FREEZE_WRITE);
  530. if (XFS_FORCED_SHUTDOWN(mp))
  531. return -EIO;
  532. relock:
  533. if (ioflags & IO_ISDIRECT) {
  534. iolock = XFS_IOLOCK_SHARED;
  535. locktype = VRWLOCK_WRITE_DIRECT;
  536. need_i_mutex = 0;
  537. } else {
  538. iolock = XFS_IOLOCK_EXCL;
  539. locktype = VRWLOCK_WRITE;
  540. need_i_mutex = 1;
  541. mutex_lock(&inode->i_mutex);
  542. }
  543. xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
  544. start:
  545. error = -generic_write_checks(file, &pos, &count,
  546. S_ISBLK(inode->i_mode));
  547. if (error) {
  548. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  549. goto out_unlock_mutex;
  550. }
  551. if ((DM_EVENT_ENABLED(xip, DM_EVENT_WRITE) &&
  552. !(ioflags & IO_INVIS) && !eventsent)) {
  553. int dmflags = FILP_DELAY_FLAG(file);
  554. if (need_i_mutex)
  555. dmflags |= DM_FLAGS_IMUX;
  556. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  557. error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
  558. pos, count,
  559. dmflags, &locktype);
  560. if (error) {
  561. goto out_unlock_internal;
  562. }
  563. xfs_ilock(xip, XFS_ILOCK_EXCL);
  564. eventsent = 1;
  565. /*
  566. * The iolock was dropped and reacquired in XFS_SEND_DATA
  567. * so we have to recheck the size when appending.
  568. * We will only "goto start;" once, since having sent the
  569. * event prevents another call to XFS_SEND_DATA, which is
  570. * what allows the size to change in the first place.
  571. */
  572. if ((file->f_flags & O_APPEND) && pos != xip->i_size)
  573. goto start;
  574. }
  575. if (ioflags & IO_ISDIRECT) {
  576. xfs_buftarg_t *target =
  577. XFS_IS_REALTIME_INODE(xip) ?
  578. mp->m_rtdev_targp : mp->m_ddev_targp;
  579. if ((pos & target->bt_smask) || (count & target->bt_smask)) {
  580. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  581. return XFS_ERROR(-EINVAL);
  582. }
  583. if (!need_i_mutex && (VN_CACHED(vp) || pos > xip->i_size)) {
  584. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  585. iolock = XFS_IOLOCK_EXCL;
  586. locktype = VRWLOCK_WRITE;
  587. need_i_mutex = 1;
  588. mutex_lock(&inode->i_mutex);
  589. xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
  590. goto start;
  591. }
  592. }
  593. new_size = pos + count;
  594. if (new_size > xip->i_size)
  595. xip->i_new_size = new_size;
  596. if (likely(!(ioflags & IO_INVIS))) {
  597. file_update_time(file);
  598. xfs_ichgtime_fast(xip, inode,
  599. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  600. }
  601. /*
  602. * If the offset is beyond the size of the file, we have a couple
  603. * of things to do. First, if there is already space allocated
  604. * we need to either create holes or zero the disk or ...
  605. *
  606. * If there is a page where the previous size lands, we need
  607. * to zero it out up to the new size.
  608. */
  609. if (pos > xip->i_size) {
  610. error = xfs_zero_eof(xip, pos, xip->i_size);
  611. if (error) {
  612. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  613. goto out_unlock_internal;
  614. }
  615. }
  616. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  617. /*
  618. * If we're writing the file then make sure to clear the
  619. * setuid and setgid bits if the process is not being run
  620. * by root. This keeps people from modifying setuid and
  621. * setgid binaries.
  622. */
  623. if (((xip->i_d.di_mode & S_ISUID) ||
  624. ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
  625. (S_ISGID | S_IXGRP))) &&
  626. !capable(CAP_FSETID)) {
  627. error = xfs_write_clear_setuid(xip);
  628. if (likely(!error))
  629. error = -remove_suid(file->f_path.dentry);
  630. if (unlikely(error)) {
  631. goto out_unlock_internal;
  632. }
  633. }
  634. retry:
  635. /* We can write back this queue in page reclaim */
  636. current->backing_dev_info = mapping->backing_dev_info;
  637. if ((ioflags & IO_ISDIRECT)) {
  638. if (VN_CACHED(vp)) {
  639. WARN_ON(need_i_mutex == 0);
  640. xfs_inval_cached_trace(xip, pos, -1,
  641. (pos & PAGE_CACHE_MASK), -1);
  642. error = xfs_flushinval_pages(xip,
  643. (pos & PAGE_CACHE_MASK),
  644. -1, FI_REMAPF_LOCKED);
  645. if (error)
  646. goto out_unlock_internal;
  647. }
  648. if (need_i_mutex) {
  649. /* demote the lock now the cached pages are gone */
  650. xfs_ilock_demote(xip, XFS_IOLOCK_EXCL);
  651. mutex_unlock(&inode->i_mutex);
  652. iolock = XFS_IOLOCK_SHARED;
  653. locktype = VRWLOCK_WRITE_DIRECT;
  654. need_i_mutex = 0;
  655. }
  656. xfs_rw_enter_trace(XFS_DIOWR_ENTER, xip, (void *)iovp, segs,
  657. *offset, ioflags);
  658. ret = generic_file_direct_write(iocb, iovp,
  659. &segs, pos, offset, count, ocount);
  660. /*
  661. * direct-io write to a hole: fall through to buffered I/O
  662. * for completing the rest of the request.
  663. */
  664. if (ret >= 0 && ret != count) {
  665. XFS_STATS_ADD(xs_write_bytes, ret);
  666. pos += ret;
  667. count -= ret;
  668. ioflags &= ~IO_ISDIRECT;
  669. xfs_iunlock(xip, iolock);
  670. goto relock;
  671. }
  672. } else {
  673. xfs_rw_enter_trace(XFS_WRITE_ENTER, xip, (void *)iovp, segs,
  674. *offset, ioflags);
  675. ret = generic_file_buffered_write(iocb, iovp, segs,
  676. pos, offset, count, ret);
  677. }
  678. current->backing_dev_info = NULL;
  679. if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
  680. ret = wait_on_sync_kiocb(iocb);
  681. if (ret == -ENOSPC &&
  682. DM_EVENT_ENABLED(xip, DM_EVENT_NOSPACE) && !(ioflags & IO_INVIS)) {
  683. xfs_rwunlock(xip, locktype);
  684. if (need_i_mutex)
  685. mutex_unlock(&inode->i_mutex);
  686. error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
  687. DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
  688. 0, 0, 0); /* Delay flag intentionally unused */
  689. if (need_i_mutex)
  690. mutex_lock(&inode->i_mutex);
  691. xfs_rwlock(xip, locktype);
  692. if (error)
  693. goto out_unlock_internal;
  694. pos = xip->i_size;
  695. ret = 0;
  696. goto retry;
  697. }
  698. isize = i_size_read(inode);
  699. if (unlikely(ret < 0 && ret != -EFAULT && *offset > isize))
  700. *offset = isize;
  701. if (*offset > xip->i_size) {
  702. xfs_ilock(xip, XFS_ILOCK_EXCL);
  703. if (*offset > xip->i_size)
  704. xip->i_size = *offset;
  705. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  706. }
  707. error = -ret;
  708. if (ret <= 0)
  709. goto out_unlock_internal;
  710. XFS_STATS_ADD(xs_write_bytes, ret);
  711. /* Handle various SYNC-type writes */
  712. if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
  713. int error2;
  714. xfs_rwunlock(xip, locktype);
  715. if (need_i_mutex)
  716. mutex_unlock(&inode->i_mutex);
  717. error2 = sync_page_range(inode, mapping, pos, ret);
  718. if (!error)
  719. error = error2;
  720. if (need_i_mutex)
  721. mutex_lock(&inode->i_mutex);
  722. xfs_rwlock(xip, locktype);
  723. error2 = xfs_write_sync_logforce(mp, xip);
  724. if (!error)
  725. error = error2;
  726. }
  727. out_unlock_internal:
  728. if (xip->i_new_size) {
  729. xfs_ilock(xip, XFS_ILOCK_EXCL);
  730. xip->i_new_size = 0;
  731. /*
  732. * If this was a direct or synchronous I/O that failed (such
  733. * as ENOSPC) then part of the I/O may have been written to
  734. * disk before the error occured. In this case the on-disk
  735. * file size may have been adjusted beyond the in-memory file
  736. * size and now needs to be truncated back.
  737. */
  738. if (xip->i_d.di_size > xip->i_size)
  739. xip->i_d.di_size = xip->i_size;
  740. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  741. }
  742. xfs_rwunlock(xip, locktype);
  743. out_unlock_mutex:
  744. if (need_i_mutex)
  745. mutex_unlock(&inode->i_mutex);
  746. return -error;
  747. }
  748. /*
  749. * All xfs metadata buffers except log state machine buffers
  750. * get this attached as their b_bdstrat callback function.
  751. * This is so that we can catch a buffer
  752. * after prematurely unpinning it to forcibly shutdown the filesystem.
  753. */
  754. int
  755. xfs_bdstrat_cb(struct xfs_buf *bp)
  756. {
  757. xfs_mount_t *mp;
  758. mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
  759. if (!XFS_FORCED_SHUTDOWN(mp)) {
  760. xfs_buf_iorequest(bp);
  761. return 0;
  762. } else {
  763. xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
  764. /*
  765. * Metadata write that didn't get logged but
  766. * written delayed anyway. These aren't associated
  767. * with a transaction, and can be ignored.
  768. */
  769. if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
  770. (XFS_BUF_ISREAD(bp)) == 0)
  771. return (xfs_bioerror_relse(bp));
  772. else
  773. return (xfs_bioerror(bp));
  774. }
  775. }
  776. /*
  777. * Wrapper around bdstrat so that we can stop data
  778. * from going to disk in case we are shutting down the filesystem.
  779. * Typically user data goes thru this path; one of the exceptions
  780. * is the superblock.
  781. */
  782. int
  783. xfsbdstrat(
  784. struct xfs_mount *mp,
  785. struct xfs_buf *bp)
  786. {
  787. ASSERT(mp);
  788. if (!XFS_FORCED_SHUTDOWN(mp)) {
  789. /* Grio redirection would go here
  790. * if (XFS_BUF_IS_GRIO(bp)) {
  791. */
  792. xfs_buf_iorequest(bp);
  793. return 0;
  794. }
  795. xfs_buftrace("XFSBDSTRAT IOERROR", bp);
  796. return (xfs_bioerror_relse(bp));
  797. }
  798. /*
  799. * If the underlying (data/log/rt) device is readonly, there are some
  800. * operations that cannot proceed.
  801. */
  802. int
  803. xfs_dev_is_read_only(
  804. xfs_mount_t *mp,
  805. char *message)
  806. {
  807. if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
  808. xfs_readonly_buftarg(mp->m_logdev_targp) ||
  809. (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
  810. cmn_err(CE_NOTE,
  811. "XFS: %s required on read-only device.", message);
  812. cmn_err(CE_NOTE,
  813. "XFS: write access unavailable, cannot proceed.");
  814. return EROFS;
  815. }
  816. return 0;
  817. }