xfs_lrw.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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(ip, 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(ip, 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 (ioflags & IO_ISAIO)
  582. return XFS_ERROR(-ENOSYS);
  583. if ((pos & target->pbr_smask) || (count & target->pbr_smask))
  584. return XFS_ERROR(-EINVAL);
  585. if (!VN_CACHED(vp) && pos < i_size_read(inode))
  586. need_isem = 0;
  587. if (VN_CACHED(vp))
  588. need_flush = 1;
  589. }
  590. relock:
  591. if (need_isem) {
  592. iolock = XFS_IOLOCK_EXCL;
  593. locktype = VRWLOCK_WRITE;
  594. down(&inode->i_sem);
  595. } else {
  596. iolock = XFS_IOLOCK_SHARED;
  597. locktype = VRWLOCK_WRITE_DIRECT;
  598. }
  599. xfs_ilock(xip, XFS_ILOCK_EXCL|iolock);
  600. isize = i_size_read(inode);
  601. if (file->f_flags & O_APPEND)
  602. *offset = isize;
  603. start:
  604. error = -generic_write_checks(file, &pos, &count,
  605. S_ISBLK(inode->i_mode));
  606. if (error) {
  607. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  608. goto out_unlock_isem;
  609. }
  610. new_size = pos + count;
  611. if (new_size > isize)
  612. io->io_new_size = new_size;
  613. if ((DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_WRITE) &&
  614. !(ioflags & IO_INVIS) && !eventsent)) {
  615. loff_t savedsize = pos;
  616. int dmflags = FILP_DELAY_FLAG(file);
  617. if (need_isem)
  618. dmflags |= DM_FLAGS_ISEM;
  619. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  620. error = XFS_SEND_DATA(xip->i_mount, DM_EVENT_WRITE, vp,
  621. pos, count,
  622. dmflags, &locktype);
  623. if (error) {
  624. xfs_iunlock(xip, iolock);
  625. goto out_unlock_isem;
  626. }
  627. xfs_ilock(xip, XFS_ILOCK_EXCL);
  628. eventsent = 1;
  629. /*
  630. * The iolock was dropped and reaquired in XFS_SEND_DATA
  631. * so we have to recheck the size when appending.
  632. * We will only "goto start;" once, since having sent the
  633. * event prevents another call to XFS_SEND_DATA, which is
  634. * what allows the size to change in the first place.
  635. */
  636. if ((file->f_flags & O_APPEND) && savedsize != isize) {
  637. pos = isize = xip->i_d.di_size;
  638. goto start;
  639. }
  640. }
  641. /*
  642. * On Linux, generic_file_write updates the times even if
  643. * no data is copied in so long as the write had a size.
  644. *
  645. * We must update xfs' times since revalidate will overcopy xfs.
  646. */
  647. if (!(ioflags & IO_INVIS)) {
  648. xfs_ichgtime(xip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  649. inode_update_time(inode, 1);
  650. }
  651. /*
  652. * If the offset is beyond the size of the file, we have a couple
  653. * of things to do. First, if there is already space allocated
  654. * we need to either create holes or zero the disk or ...
  655. *
  656. * If there is a page where the previous size lands, we need
  657. * to zero it out up to the new size.
  658. */
  659. if (pos > isize) {
  660. error = xfs_zero_eof(BHV_TO_VNODE(bdp), io, pos,
  661. isize, pos + count);
  662. if (error) {
  663. xfs_iunlock(xip, XFS_ILOCK_EXCL|iolock);
  664. goto out_unlock_isem;
  665. }
  666. }
  667. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  668. /*
  669. * If we're writing the file then make sure to clear the
  670. * setuid and setgid bits if the process is not being run
  671. * by root. This keeps people from modifying setuid and
  672. * setgid binaries.
  673. */
  674. if (((xip->i_d.di_mode & S_ISUID) ||
  675. ((xip->i_d.di_mode & (S_ISGID | S_IXGRP)) ==
  676. (S_ISGID | S_IXGRP))) &&
  677. !capable(CAP_FSETID)) {
  678. error = xfs_write_clear_setuid(xip);
  679. if (likely(!error))
  680. error = -remove_suid(file->f_dentry);
  681. if (unlikely(error)) {
  682. xfs_iunlock(xip, iolock);
  683. goto out_unlock_isem;
  684. }
  685. }
  686. retry:
  687. /* We can write back this queue in page reclaim */
  688. current->backing_dev_info = mapping->backing_dev_info;
  689. if ((ioflags & IO_ISDIRECT)) {
  690. if (need_flush) {
  691. xfs_inval_cached_trace(io, pos, -1,
  692. ctooff(offtoct(pos)), -1);
  693. VOP_FLUSHINVAL_PAGES(vp, ctooff(offtoct(pos)),
  694. -1, FI_REMAPF_LOCKED);
  695. }
  696. if (need_isem) {
  697. /* demote the lock now the cached pages are gone */
  698. XFS_ILOCK_DEMOTE(mp, io, XFS_IOLOCK_EXCL);
  699. up(&inode->i_sem);
  700. iolock = XFS_IOLOCK_SHARED;
  701. locktype = VRWLOCK_WRITE_DIRECT;
  702. need_isem = 0;
  703. }
  704. xfs_rw_enter_trace(XFS_DIOWR_ENTER, io, (void *)iovp, segs,
  705. *offset, ioflags);
  706. ret = generic_file_direct_write(iocb, iovp,
  707. &segs, pos, offset, count, ocount);
  708. /*
  709. * direct-io write to a hole: fall through to buffered I/O
  710. * for completing the rest of the request.
  711. */
  712. if (ret >= 0 && ret != count) {
  713. XFS_STATS_ADD(xs_write_bytes, ret);
  714. pos += ret;
  715. count -= ret;
  716. need_isem = 1;
  717. ioflags &= ~IO_ISDIRECT;
  718. xfs_iunlock(xip, iolock);
  719. goto relock;
  720. }
  721. } else {
  722. xfs_rw_enter_trace(XFS_WRITE_ENTER, io, (void *)iovp, segs,
  723. *offset, ioflags);
  724. ret = generic_file_buffered_write(iocb, iovp, segs,
  725. pos, offset, count, ret);
  726. }
  727. current->backing_dev_info = NULL;
  728. if (ret == -EIOCBQUEUED && !(ioflags & IO_ISAIO))
  729. ret = wait_on_sync_kiocb(iocb);
  730. if ((ret == -ENOSPC) &&
  731. DM_EVENT_ENABLED(vp->v_vfsp, xip, DM_EVENT_NOSPACE) &&
  732. !(ioflags & IO_INVIS)) {
  733. xfs_rwunlock(bdp, locktype);
  734. if (need_isem)
  735. up(&inode->i_sem);
  736. error = XFS_SEND_NAMESP(xip->i_mount, DM_EVENT_NOSPACE, vp,
  737. DM_RIGHT_NULL, vp, DM_RIGHT_NULL, NULL, NULL,
  738. 0, 0, 0); /* Delay flag intentionally unused */
  739. if (error)
  740. goto out_nounlocks;
  741. if (need_isem)
  742. down(&inode->i_sem);
  743. xfs_rwlock(bdp, locktype);
  744. pos = xip->i_d.di_size;
  745. ret = 0;
  746. goto retry;
  747. }
  748. if (*offset > xip->i_d.di_size) {
  749. xfs_ilock(xip, XFS_ILOCK_EXCL);
  750. if (*offset > xip->i_d.di_size) {
  751. xip->i_d.di_size = *offset;
  752. i_size_write(inode, *offset);
  753. xip->i_update_core = 1;
  754. xip->i_update_size = 1;
  755. }
  756. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  757. }
  758. error = -ret;
  759. if (ret <= 0)
  760. goto out_unlock_internal;
  761. XFS_STATS_ADD(xs_write_bytes, ret);
  762. /* Handle various SYNC-type writes */
  763. if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
  764. /*
  765. * If we're treating this as O_DSYNC and we have not updated the
  766. * size, force the log.
  767. */
  768. if (!(mp->m_flags & XFS_MOUNT_OSYNCISOSYNC) &&
  769. !(xip->i_update_size)) {
  770. xfs_inode_log_item_t *iip = xip->i_itemp;
  771. /*
  772. * If an allocation transaction occurred
  773. * without extending the size, then we have to force
  774. * the log up the proper point to ensure that the
  775. * allocation is permanent. We can't count on
  776. * the fact that buffered writes lock out direct I/O
  777. * writes - the direct I/O write could have extended
  778. * the size nontransactionally, then finished before
  779. * we started. xfs_write_file will think that the file
  780. * didn't grow but the update isn't safe unless the
  781. * size change is logged.
  782. *
  783. * Force the log if we've committed a transaction
  784. * against the inode or if someone else has and
  785. * the commit record hasn't gone to disk (e.g.
  786. * the inode is pinned). This guarantees that
  787. * all changes affecting the inode are permanent
  788. * when we return.
  789. */
  790. if (iip && iip->ili_last_lsn) {
  791. xfs_log_force(mp, iip->ili_last_lsn,
  792. XFS_LOG_FORCE | XFS_LOG_SYNC);
  793. } else if (xfs_ipincount(xip) > 0) {
  794. xfs_log_force(mp, (xfs_lsn_t)0,
  795. XFS_LOG_FORCE | XFS_LOG_SYNC);
  796. }
  797. } else {
  798. xfs_trans_t *tp;
  799. /*
  800. * O_SYNC or O_DSYNC _with_ a size update are handled
  801. * the same way.
  802. *
  803. * If the write was synchronous then we need to make
  804. * sure that the inode modification time is permanent.
  805. * We'll have updated the timestamp above, so here
  806. * we use a synchronous transaction to log the inode.
  807. * It's not fast, but it's necessary.
  808. *
  809. * If this a dsync write and the size got changed
  810. * non-transactionally, then we need to ensure that
  811. * the size change gets logged in a synchronous
  812. * transaction.
  813. */
  814. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITE_SYNC);
  815. if ((error = xfs_trans_reserve(tp, 0,
  816. XFS_SWRITE_LOG_RES(mp),
  817. 0, 0, 0))) {
  818. /* Transaction reserve failed */
  819. xfs_trans_cancel(tp, 0);
  820. } else {
  821. /* Transaction reserve successful */
  822. xfs_ilock(xip, XFS_ILOCK_EXCL);
  823. xfs_trans_ijoin(tp, xip, XFS_ILOCK_EXCL);
  824. xfs_trans_ihold(tp, xip);
  825. xfs_trans_log_inode(tp, xip, XFS_ILOG_CORE);
  826. xfs_trans_set_sync(tp);
  827. error = xfs_trans_commit(tp, 0, NULL);
  828. xfs_iunlock(xip, XFS_ILOCK_EXCL);
  829. }
  830. if (error)
  831. goto out_unlock_internal;
  832. }
  833. xfs_rwunlock(bdp, locktype);
  834. if (need_isem)
  835. up(&inode->i_sem);
  836. error = sync_page_range(inode, mapping, pos, ret);
  837. if (!error)
  838. error = ret;
  839. return error;
  840. }
  841. out_unlock_internal:
  842. xfs_rwunlock(bdp, locktype);
  843. out_unlock_isem:
  844. if (need_isem)
  845. up(&inode->i_sem);
  846. out_nounlocks:
  847. return -error;
  848. }
  849. /*
  850. * All xfs metadata buffers except log state machine buffers
  851. * get this attached as their b_bdstrat callback function.
  852. * This is so that we can catch a buffer
  853. * after prematurely unpinning it to forcibly shutdown the filesystem.
  854. */
  855. int
  856. xfs_bdstrat_cb(struct xfs_buf *bp)
  857. {
  858. xfs_mount_t *mp;
  859. mp = XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *);
  860. if (!XFS_FORCED_SHUTDOWN(mp)) {
  861. pagebuf_iorequest(bp);
  862. return 0;
  863. } else {
  864. xfs_buftrace("XFS__BDSTRAT IOERROR", bp);
  865. /*
  866. * Metadata write that didn't get logged but
  867. * written delayed anyway. These aren't associated
  868. * with a transaction, and can be ignored.
  869. */
  870. if (XFS_BUF_IODONE_FUNC(bp) == NULL &&
  871. (XFS_BUF_ISREAD(bp)) == 0)
  872. return (xfs_bioerror_relse(bp));
  873. else
  874. return (xfs_bioerror(bp));
  875. }
  876. }
  877. int
  878. xfs_bmap(bhv_desc_t *bdp,
  879. xfs_off_t offset,
  880. ssize_t count,
  881. int flags,
  882. xfs_iomap_t *iomapp,
  883. int *niomaps)
  884. {
  885. xfs_inode_t *ip = XFS_BHVTOI(bdp);
  886. xfs_iocore_t *io = &ip->i_iocore;
  887. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
  888. ASSERT(((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) != 0) ==
  889. ((ip->i_iocore.io_flags & XFS_IOCORE_RT) != 0));
  890. return xfs_iomap(io, offset, count, flags, iomapp, niomaps);
  891. }
  892. /*
  893. * Wrapper around bdstrat so that we can stop data
  894. * from going to disk in case we are shutting down the filesystem.
  895. * Typically user data goes thru this path; one of the exceptions
  896. * is the superblock.
  897. */
  898. int
  899. xfsbdstrat(
  900. struct xfs_mount *mp,
  901. struct xfs_buf *bp)
  902. {
  903. ASSERT(mp);
  904. if (!XFS_FORCED_SHUTDOWN(mp)) {
  905. /* Grio redirection would go here
  906. * if (XFS_BUF_IS_GRIO(bp)) {
  907. */
  908. pagebuf_iorequest(bp);
  909. return 0;
  910. }
  911. xfs_buftrace("XFSBDSTRAT IOERROR", bp);
  912. return (xfs_bioerror_relse(bp));
  913. }
  914. /*
  915. * If the underlying (data/log/rt) device is readonly, there are some
  916. * operations that cannot proceed.
  917. */
  918. int
  919. xfs_dev_is_read_only(
  920. xfs_mount_t *mp,
  921. char *message)
  922. {
  923. if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
  924. xfs_readonly_buftarg(mp->m_logdev_targp) ||
  925. (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
  926. cmn_err(CE_NOTE,
  927. "XFS: %s required on read-only device.", message);
  928. cmn_err(CE_NOTE,
  929. "XFS: write access unavailable, cannot proceed.");
  930. return EROFS;
  931. }
  932. return 0;
  933. }