xfs_file.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  2. * Copyright (c) 2000-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_sb.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_alloc.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_inode_item.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_error.h"
  34. #include "xfs_vnodeops.h"
  35. #include "xfs_da_btree.h"
  36. #include "xfs_ioctl.h"
  37. #include "xfs_trace.h"
  38. #include <linux/dcache.h>
  39. #include <linux/falloc.h>
  40. static const struct vm_operations_struct xfs_file_vm_ops;
  41. /*
  42. * Locking primitives for read and write IO paths to ensure we consistently use
  43. * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
  44. */
  45. static inline void
  46. xfs_rw_ilock(
  47. struct xfs_inode *ip,
  48. int type)
  49. {
  50. if (type & XFS_IOLOCK_EXCL)
  51. mutex_lock(&VFS_I(ip)->i_mutex);
  52. xfs_ilock(ip, type);
  53. }
  54. static inline void
  55. xfs_rw_iunlock(
  56. struct xfs_inode *ip,
  57. int type)
  58. {
  59. xfs_iunlock(ip, type);
  60. if (type & XFS_IOLOCK_EXCL)
  61. mutex_unlock(&VFS_I(ip)->i_mutex);
  62. }
  63. static inline void
  64. xfs_rw_ilock_demote(
  65. struct xfs_inode *ip,
  66. int type)
  67. {
  68. xfs_ilock_demote(ip, type);
  69. if (type & XFS_IOLOCK_EXCL)
  70. mutex_unlock(&VFS_I(ip)->i_mutex);
  71. }
  72. /*
  73. * xfs_iozero
  74. *
  75. * xfs_iozero clears the specified range of buffer supplied,
  76. * and marks all the affected blocks as valid and modified. If
  77. * an affected block is not allocated, it will be allocated. If
  78. * an affected block is not completely overwritten, and is not
  79. * valid before the operation, it will be read from disk before
  80. * being partially zeroed.
  81. */
  82. STATIC int
  83. xfs_iozero(
  84. struct xfs_inode *ip, /* inode */
  85. loff_t pos, /* offset in file */
  86. size_t count) /* size of data to zero */
  87. {
  88. struct page *page;
  89. struct address_space *mapping;
  90. int status;
  91. mapping = VFS_I(ip)->i_mapping;
  92. do {
  93. unsigned offset, bytes;
  94. void *fsdata;
  95. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  96. bytes = PAGE_CACHE_SIZE - offset;
  97. if (bytes > count)
  98. bytes = count;
  99. status = pagecache_write_begin(NULL, mapping, pos, bytes,
  100. AOP_FLAG_UNINTERRUPTIBLE,
  101. &page, &fsdata);
  102. if (status)
  103. break;
  104. zero_user(page, offset, bytes);
  105. status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
  106. page, fsdata);
  107. WARN_ON(status <= 0); /* can't return less than zero! */
  108. pos += bytes;
  109. count -= bytes;
  110. status = 0;
  111. } while (count);
  112. return (-status);
  113. }
  114. STATIC int
  115. xfs_file_fsync(
  116. struct file *file,
  117. int datasync)
  118. {
  119. struct inode *inode = file->f_mapping->host;
  120. struct xfs_inode *ip = XFS_I(inode);
  121. struct xfs_trans *tp;
  122. int error = 0;
  123. int log_flushed = 0;
  124. trace_xfs_file_fsync(ip);
  125. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  126. return -XFS_ERROR(EIO);
  127. xfs_iflags_clear(ip, XFS_ITRUNCATED);
  128. xfs_ioend_wait(ip);
  129. /*
  130. * We always need to make sure that the required inode state is safe on
  131. * disk. The inode might be clean but we still might need to force the
  132. * log because of committed transactions that haven't hit the disk yet.
  133. * Likewise, there could be unflushed non-transactional changes to the
  134. * inode core that have to go to disk and this requires us to issue
  135. * a synchronous transaction to capture these changes correctly.
  136. *
  137. * This code relies on the assumption that if the i_update_core field
  138. * of the inode is clear and the inode is unpinned then it is clean
  139. * and no action is required.
  140. */
  141. xfs_ilock(ip, XFS_ILOCK_SHARED);
  142. /*
  143. * First check if the VFS inode is marked dirty. All the dirtying
  144. * of non-transactional updates no goes through mark_inode_dirty*,
  145. * which allows us to distinguish beteeen pure timestamp updates
  146. * and i_size updates which need to be caught for fdatasync.
  147. * After that also theck for the dirty state in the XFS inode, which
  148. * might gets cleared when the inode gets written out via the AIL
  149. * or xfs_iflush_cluster.
  150. */
  151. if (((inode->i_state & I_DIRTY_DATASYNC) ||
  152. ((inode->i_state & I_DIRTY_SYNC) && !datasync)) &&
  153. ip->i_update_core) {
  154. /*
  155. * Kick off a transaction to log the inode core to get the
  156. * updates. The sync transaction will also force the log.
  157. */
  158. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  159. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  160. error = xfs_trans_reserve(tp, 0,
  161. XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
  162. if (error) {
  163. xfs_trans_cancel(tp, 0);
  164. return -error;
  165. }
  166. xfs_ilock(ip, XFS_ILOCK_EXCL);
  167. /*
  168. * Note - it's possible that we might have pushed ourselves out
  169. * of the way during trans_reserve which would flush the inode.
  170. * But there's no guarantee that the inode buffer has actually
  171. * gone out yet (it's delwri). Plus the buffer could be pinned
  172. * anyway if it's part of an inode in another recent
  173. * transaction. So we play it safe and fire off the
  174. * transaction anyway.
  175. */
  176. xfs_trans_ijoin(tp, ip);
  177. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  178. xfs_trans_set_sync(tp);
  179. error = _xfs_trans_commit(tp, 0, &log_flushed);
  180. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  181. } else {
  182. /*
  183. * Timestamps/size haven't changed since last inode flush or
  184. * inode transaction commit. That means either nothing got
  185. * written or a transaction committed which caught the updates.
  186. * If the latter happened and the transaction hasn't hit the
  187. * disk yet, the inode will be still be pinned. If it is,
  188. * force the log.
  189. */
  190. if (xfs_ipincount(ip)) {
  191. error = _xfs_log_force_lsn(ip->i_mount,
  192. ip->i_itemp->ili_last_lsn,
  193. XFS_LOG_SYNC, &log_flushed);
  194. }
  195. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  196. }
  197. if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
  198. /*
  199. * If the log write didn't issue an ordered tag we need
  200. * to flush the disk cache for the data device now.
  201. */
  202. if (!log_flushed)
  203. xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
  204. /*
  205. * If this inode is on the RT dev we need to flush that
  206. * cache as well.
  207. */
  208. if (XFS_IS_REALTIME_INODE(ip))
  209. xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
  210. }
  211. return -error;
  212. }
  213. STATIC ssize_t
  214. xfs_file_aio_read(
  215. struct kiocb *iocb,
  216. const struct iovec *iovp,
  217. unsigned long nr_segs,
  218. loff_t pos)
  219. {
  220. struct file *file = iocb->ki_filp;
  221. struct inode *inode = file->f_mapping->host;
  222. struct xfs_inode *ip = XFS_I(inode);
  223. struct xfs_mount *mp = ip->i_mount;
  224. size_t size = 0;
  225. ssize_t ret = 0;
  226. int ioflags = 0;
  227. xfs_fsize_t n;
  228. unsigned long seg;
  229. XFS_STATS_INC(xs_read_calls);
  230. BUG_ON(iocb->ki_pos != pos);
  231. if (unlikely(file->f_flags & O_DIRECT))
  232. ioflags |= IO_ISDIRECT;
  233. if (file->f_mode & FMODE_NOCMTIME)
  234. ioflags |= IO_INVIS;
  235. /* START copy & waste from filemap.c */
  236. for (seg = 0; seg < nr_segs; seg++) {
  237. const struct iovec *iv = &iovp[seg];
  238. /*
  239. * If any segment has a negative length, or the cumulative
  240. * length ever wraps negative then return -EINVAL.
  241. */
  242. size += iv->iov_len;
  243. if (unlikely((ssize_t)(size|iv->iov_len) < 0))
  244. return XFS_ERROR(-EINVAL);
  245. }
  246. /* END copy & waste from filemap.c */
  247. if (unlikely(ioflags & IO_ISDIRECT)) {
  248. xfs_buftarg_t *target =
  249. XFS_IS_REALTIME_INODE(ip) ?
  250. mp->m_rtdev_targp : mp->m_ddev_targp;
  251. if ((iocb->ki_pos & target->bt_smask) ||
  252. (size & target->bt_smask)) {
  253. if (iocb->ki_pos == ip->i_size)
  254. return 0;
  255. return -XFS_ERROR(EINVAL);
  256. }
  257. }
  258. n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
  259. if (n <= 0 || size == 0)
  260. return 0;
  261. if (n < size)
  262. size = n;
  263. if (XFS_FORCED_SHUTDOWN(mp))
  264. return -EIO;
  265. if (unlikely(ioflags & IO_ISDIRECT)) {
  266. xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
  267. if (inode->i_mapping->nrpages) {
  268. ret = -xfs_flushinval_pages(ip,
  269. (iocb->ki_pos & PAGE_CACHE_MASK),
  270. -1, FI_REMAPF_LOCKED);
  271. if (ret) {
  272. xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
  273. return ret;
  274. }
  275. }
  276. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  277. } else
  278. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  279. trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
  280. ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
  281. if (ret > 0)
  282. XFS_STATS_ADD(xs_read_bytes, ret);
  283. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  284. return ret;
  285. }
  286. STATIC ssize_t
  287. xfs_file_splice_read(
  288. struct file *infilp,
  289. loff_t *ppos,
  290. struct pipe_inode_info *pipe,
  291. size_t count,
  292. unsigned int flags)
  293. {
  294. struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
  295. int ioflags = 0;
  296. ssize_t ret;
  297. XFS_STATS_INC(xs_read_calls);
  298. if (infilp->f_mode & FMODE_NOCMTIME)
  299. ioflags |= IO_INVIS;
  300. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  301. return -EIO;
  302. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  303. trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
  304. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  305. if (ret > 0)
  306. XFS_STATS_ADD(xs_read_bytes, ret);
  307. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  308. return ret;
  309. }
  310. STATIC void
  311. xfs_aio_write_isize_update(
  312. struct inode *inode,
  313. loff_t *ppos,
  314. ssize_t bytes_written)
  315. {
  316. struct xfs_inode *ip = XFS_I(inode);
  317. xfs_fsize_t isize = i_size_read(inode);
  318. if (bytes_written > 0)
  319. XFS_STATS_ADD(xs_write_bytes, bytes_written);
  320. if (unlikely(bytes_written < 0 && bytes_written != -EFAULT &&
  321. *ppos > isize))
  322. *ppos = isize;
  323. if (*ppos > ip->i_size) {
  324. xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
  325. if (*ppos > ip->i_size)
  326. ip->i_size = *ppos;
  327. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  328. }
  329. }
  330. /*
  331. * If this was a direct or synchronous I/O that failed (such as ENOSPC) then
  332. * part of the I/O may have been written to disk before the error occured. In
  333. * this case the on-disk file size may have been adjusted beyond the in-memory
  334. * file size and now needs to be truncated back.
  335. */
  336. STATIC void
  337. xfs_aio_write_newsize_update(
  338. struct xfs_inode *ip)
  339. {
  340. if (ip->i_new_size) {
  341. xfs_rw_ilock(ip, XFS_ILOCK_EXCL);
  342. ip->i_new_size = 0;
  343. if (ip->i_d.di_size > ip->i_size)
  344. ip->i_d.di_size = ip->i_size;
  345. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  346. }
  347. }
  348. /*
  349. * xfs_file_splice_write() does not use xfs_rw_ilock() because
  350. * generic_file_splice_write() takes the i_mutex itself. This, in theory,
  351. * couuld cause lock inversions between the aio_write path and the splice path
  352. * if someone is doing concurrent splice(2) based writes and write(2) based
  353. * writes to the same inode. The only real way to fix this is to re-implement
  354. * the generic code here with correct locking orders.
  355. */
  356. STATIC ssize_t
  357. xfs_file_splice_write(
  358. struct pipe_inode_info *pipe,
  359. struct file *outfilp,
  360. loff_t *ppos,
  361. size_t count,
  362. unsigned int flags)
  363. {
  364. struct inode *inode = outfilp->f_mapping->host;
  365. struct xfs_inode *ip = XFS_I(inode);
  366. xfs_fsize_t new_size;
  367. int ioflags = 0;
  368. ssize_t ret;
  369. XFS_STATS_INC(xs_write_calls);
  370. if (outfilp->f_mode & FMODE_NOCMTIME)
  371. ioflags |= IO_INVIS;
  372. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  373. return -EIO;
  374. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  375. new_size = *ppos + count;
  376. xfs_ilock(ip, XFS_ILOCK_EXCL);
  377. if (new_size > ip->i_size)
  378. ip->i_new_size = new_size;
  379. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  380. trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
  381. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  382. xfs_aio_write_isize_update(inode, ppos, ret);
  383. xfs_aio_write_newsize_update(ip);
  384. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  385. return ret;
  386. }
  387. /*
  388. * This routine is called to handle zeroing any space in the last
  389. * block of the file that is beyond the EOF. We do this since the
  390. * size is being increased without writing anything to that block
  391. * and we don't want anyone to read the garbage on the disk.
  392. */
  393. STATIC int /* error (positive) */
  394. xfs_zero_last_block(
  395. xfs_inode_t *ip,
  396. xfs_fsize_t offset,
  397. xfs_fsize_t isize)
  398. {
  399. xfs_fileoff_t last_fsb;
  400. xfs_mount_t *mp = ip->i_mount;
  401. int nimaps;
  402. int zero_offset;
  403. int zero_len;
  404. int error = 0;
  405. xfs_bmbt_irec_t imap;
  406. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  407. zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  408. if (zero_offset == 0) {
  409. /*
  410. * There are no extra bytes in the last block on disk to
  411. * zero, so return.
  412. */
  413. return 0;
  414. }
  415. last_fsb = XFS_B_TO_FSBT(mp, isize);
  416. nimaps = 1;
  417. error = xfs_bmapi(NULL, ip, last_fsb, 1, 0, NULL, 0, &imap,
  418. &nimaps, NULL);
  419. if (error) {
  420. return error;
  421. }
  422. ASSERT(nimaps > 0);
  423. /*
  424. * If the block underlying isize is just a hole, then there
  425. * is nothing to zero.
  426. */
  427. if (imap.br_startblock == HOLESTARTBLOCK) {
  428. return 0;
  429. }
  430. /*
  431. * Zero the part of the last block beyond the EOF, and write it
  432. * out sync. We need to drop the ilock while we do this so we
  433. * don't deadlock when the buffer cache calls back to us.
  434. */
  435. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  436. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  437. if (isize + zero_len > offset)
  438. zero_len = offset - isize;
  439. error = xfs_iozero(ip, isize, zero_len);
  440. xfs_ilock(ip, XFS_ILOCK_EXCL);
  441. ASSERT(error >= 0);
  442. return error;
  443. }
  444. /*
  445. * Zero any on disk space between the current EOF and the new,
  446. * larger EOF. This handles the normal case of zeroing the remainder
  447. * of the last block in the file and the unusual case of zeroing blocks
  448. * out beyond the size of the file. This second case only happens
  449. * with fixed size extents and when the system crashes before the inode
  450. * size was updated but after blocks were allocated. If fill is set,
  451. * then any holes in the range are filled and zeroed. If not, the holes
  452. * are left alone as holes.
  453. */
  454. int /* error (positive) */
  455. xfs_zero_eof(
  456. xfs_inode_t *ip,
  457. xfs_off_t offset, /* starting I/O offset */
  458. xfs_fsize_t isize) /* current inode size */
  459. {
  460. xfs_mount_t *mp = ip->i_mount;
  461. xfs_fileoff_t start_zero_fsb;
  462. xfs_fileoff_t end_zero_fsb;
  463. xfs_fileoff_t zero_count_fsb;
  464. xfs_fileoff_t last_fsb;
  465. xfs_fileoff_t zero_off;
  466. xfs_fsize_t zero_len;
  467. int nimaps;
  468. int error = 0;
  469. xfs_bmbt_irec_t imap;
  470. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  471. ASSERT(offset > isize);
  472. /*
  473. * First handle zeroing the block on which isize resides.
  474. * We only zero a part of that block so it is handled specially.
  475. */
  476. error = xfs_zero_last_block(ip, offset, isize);
  477. if (error) {
  478. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  479. return error;
  480. }
  481. /*
  482. * Calculate the range between the new size and the old
  483. * where blocks needing to be zeroed may exist. To get the
  484. * block where the last byte in the file currently resides,
  485. * we need to subtract one from the size and truncate back
  486. * to a block boundary. We subtract 1 in case the size is
  487. * exactly on a block boundary.
  488. */
  489. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  490. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  491. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  492. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  493. if (last_fsb == end_zero_fsb) {
  494. /*
  495. * The size was only incremented on its last block.
  496. * We took care of that above, so just return.
  497. */
  498. return 0;
  499. }
  500. ASSERT(start_zero_fsb <= end_zero_fsb);
  501. while (start_zero_fsb <= end_zero_fsb) {
  502. nimaps = 1;
  503. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  504. error = xfs_bmapi(NULL, ip, start_zero_fsb, zero_count_fsb,
  505. 0, NULL, 0, &imap, &nimaps, NULL);
  506. if (error) {
  507. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  508. return error;
  509. }
  510. ASSERT(nimaps > 0);
  511. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  512. imap.br_startblock == HOLESTARTBLOCK) {
  513. /*
  514. * This loop handles initializing pages that were
  515. * partially initialized by the code below this
  516. * loop. It basically zeroes the part of the page
  517. * that sits on a hole and sets the page as P_HOLE
  518. * and calls remapf if it is a mapped file.
  519. */
  520. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  521. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  522. continue;
  523. }
  524. /*
  525. * There are blocks we need to zero.
  526. * Drop the inode lock while we're doing the I/O.
  527. * We'll still have the iolock to protect us.
  528. */
  529. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  530. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  531. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  532. if ((zero_off + zero_len) > offset)
  533. zero_len = offset - zero_off;
  534. error = xfs_iozero(ip, zero_off, zero_len);
  535. if (error) {
  536. goto out_lock;
  537. }
  538. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  539. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  540. xfs_ilock(ip, XFS_ILOCK_EXCL);
  541. }
  542. return 0;
  543. out_lock:
  544. xfs_ilock(ip, XFS_ILOCK_EXCL);
  545. ASSERT(error >= 0);
  546. return error;
  547. }
  548. /*
  549. * Common pre-write limit and setup checks.
  550. *
  551. * Returns with iolock held according to @iolock.
  552. */
  553. STATIC ssize_t
  554. xfs_file_aio_write_checks(
  555. struct file *file,
  556. loff_t *pos,
  557. size_t *count,
  558. int *iolock)
  559. {
  560. struct inode *inode = file->f_mapping->host;
  561. struct xfs_inode *ip = XFS_I(inode);
  562. xfs_fsize_t new_size;
  563. int error = 0;
  564. error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
  565. if (error) {
  566. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
  567. *iolock = 0;
  568. return error;
  569. }
  570. new_size = *pos + *count;
  571. if (new_size > ip->i_size)
  572. ip->i_new_size = new_size;
  573. if (likely(!(file->f_mode & FMODE_NOCMTIME)))
  574. file_update_time(file);
  575. /*
  576. * If the offset is beyond the size of the file, we need to zero any
  577. * blocks that fall between the existing EOF and the start of this
  578. * write.
  579. */
  580. if (*pos > ip->i_size)
  581. error = -xfs_zero_eof(ip, *pos, ip->i_size);
  582. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  583. if (error)
  584. return error;
  585. /*
  586. * If we're writing the file then make sure to clear the setuid and
  587. * setgid bits if the process is not being run by root. This keeps
  588. * people from modifying setuid and setgid binaries.
  589. */
  590. return file_remove_suid(file);
  591. }
  592. /*
  593. * xfs_file_dio_aio_write - handle direct IO writes
  594. *
  595. * Lock the inode appropriately to prepare for and issue a direct IO write.
  596. * By separating it from the buffered write path we remove all the tricky to
  597. * follow locking changes and looping.
  598. *
  599. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  600. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  601. * pages are flushed out.
  602. *
  603. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  604. * allowing them to be done in parallel with reads and other direct IO writes.
  605. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  606. * needs to do sub-block zeroing and that requires serialisation against other
  607. * direct IOs to the same block. In this case we need to serialise the
  608. * submission of the unaligned IOs so that we don't get racing block zeroing in
  609. * the dio layer. To avoid the problem with aio, we also need to wait for
  610. * outstanding IOs to complete so that unwritten extent conversion is completed
  611. * before we try to map the overlapping block. This is currently implemented by
  612. * hitting it with a big hammer (i.e. xfs_ioend_wait()).
  613. *
  614. * Returns with locks held indicated by @iolock and errors indicated by
  615. * negative return values.
  616. */
  617. STATIC ssize_t
  618. xfs_file_dio_aio_write(
  619. struct kiocb *iocb,
  620. const struct iovec *iovp,
  621. unsigned long nr_segs,
  622. loff_t pos,
  623. size_t ocount,
  624. int *iolock)
  625. {
  626. struct file *file = iocb->ki_filp;
  627. struct address_space *mapping = file->f_mapping;
  628. struct inode *inode = mapping->host;
  629. struct xfs_inode *ip = XFS_I(inode);
  630. struct xfs_mount *mp = ip->i_mount;
  631. ssize_t ret = 0;
  632. size_t count = ocount;
  633. int unaligned_io = 0;
  634. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  635. mp->m_rtdev_targp : mp->m_ddev_targp;
  636. *iolock = 0;
  637. if ((pos & target->bt_smask) || (count & target->bt_smask))
  638. return -XFS_ERROR(EINVAL);
  639. if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
  640. unaligned_io = 1;
  641. if (unaligned_io || mapping->nrpages || pos > ip->i_size)
  642. *iolock = XFS_IOLOCK_EXCL;
  643. else
  644. *iolock = XFS_IOLOCK_SHARED;
  645. xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
  646. ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
  647. if (ret)
  648. return ret;
  649. if (mapping->nrpages) {
  650. WARN_ON(*iolock != XFS_IOLOCK_EXCL);
  651. ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
  652. FI_REMAPF_LOCKED);
  653. if (ret)
  654. return ret;
  655. }
  656. /*
  657. * If we are doing unaligned IO, wait for all other IO to drain,
  658. * otherwise demote the lock if we had to flush cached pages
  659. */
  660. if (unaligned_io)
  661. xfs_ioend_wait(ip);
  662. else if (*iolock == XFS_IOLOCK_EXCL) {
  663. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  664. *iolock = XFS_IOLOCK_SHARED;
  665. }
  666. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
  667. ret = generic_file_direct_write(iocb, iovp,
  668. &nr_segs, pos, &iocb->ki_pos, count, ocount);
  669. /* No fallback to buffered IO on errors for XFS. */
  670. ASSERT(ret < 0 || ret == count);
  671. return ret;
  672. }
  673. STATIC ssize_t
  674. xfs_file_buffered_aio_write(
  675. struct kiocb *iocb,
  676. const struct iovec *iovp,
  677. unsigned long nr_segs,
  678. loff_t pos,
  679. size_t ocount,
  680. int *iolock)
  681. {
  682. struct file *file = iocb->ki_filp;
  683. struct address_space *mapping = file->f_mapping;
  684. struct inode *inode = mapping->host;
  685. struct xfs_inode *ip = XFS_I(inode);
  686. ssize_t ret;
  687. int enospc = 0;
  688. size_t count = ocount;
  689. *iolock = XFS_IOLOCK_EXCL;
  690. xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
  691. ret = xfs_file_aio_write_checks(file, &pos, &count, iolock);
  692. if (ret)
  693. return ret;
  694. /* We can write back this queue in page reclaim */
  695. current->backing_dev_info = mapping->backing_dev_info;
  696. write_retry:
  697. trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
  698. ret = generic_file_buffered_write(iocb, iovp, nr_segs,
  699. pos, &iocb->ki_pos, count, ret);
  700. /*
  701. * if we just got an ENOSPC, flush the inode now we aren't holding any
  702. * page locks and retry *once*
  703. */
  704. if (ret == -ENOSPC && !enospc) {
  705. ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
  706. if (ret)
  707. return ret;
  708. enospc = 1;
  709. goto write_retry;
  710. }
  711. current->backing_dev_info = NULL;
  712. return ret;
  713. }
  714. STATIC ssize_t
  715. xfs_file_aio_write(
  716. struct kiocb *iocb,
  717. const struct iovec *iovp,
  718. unsigned long nr_segs,
  719. loff_t pos)
  720. {
  721. struct file *file = iocb->ki_filp;
  722. struct address_space *mapping = file->f_mapping;
  723. struct inode *inode = mapping->host;
  724. struct xfs_inode *ip = XFS_I(inode);
  725. ssize_t ret;
  726. int iolock;
  727. size_t ocount = 0;
  728. XFS_STATS_INC(xs_write_calls);
  729. BUG_ON(iocb->ki_pos != pos);
  730. ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
  731. if (ret)
  732. return ret;
  733. if (ocount == 0)
  734. return 0;
  735. xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
  736. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  737. return -EIO;
  738. if (unlikely(file->f_flags & O_DIRECT))
  739. ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
  740. ocount, &iolock);
  741. else
  742. ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
  743. ocount, &iolock);
  744. xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
  745. if (ret <= 0)
  746. goto out_unlock;
  747. /* Handle various SYNC-type writes */
  748. if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
  749. loff_t end = pos + ret - 1;
  750. int error, error2;
  751. xfs_rw_iunlock(ip, iolock);
  752. error = filemap_write_and_wait_range(mapping, pos, end);
  753. xfs_rw_ilock(ip, iolock);
  754. error2 = -xfs_file_fsync(file,
  755. (file->f_flags & __O_SYNC) ? 0 : 1);
  756. if (error)
  757. ret = error;
  758. else if (error2)
  759. ret = error2;
  760. }
  761. out_unlock:
  762. xfs_aio_write_newsize_update(ip);
  763. xfs_rw_iunlock(ip, iolock);
  764. return ret;
  765. }
  766. STATIC long
  767. xfs_file_fallocate(
  768. struct file *file,
  769. int mode,
  770. loff_t offset,
  771. loff_t len)
  772. {
  773. struct inode *inode = file->f_path.dentry->d_inode;
  774. long error;
  775. loff_t new_size = 0;
  776. xfs_flock64_t bf;
  777. xfs_inode_t *ip = XFS_I(inode);
  778. int cmd = XFS_IOC_RESVSP;
  779. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  780. return -EOPNOTSUPP;
  781. bf.l_whence = 0;
  782. bf.l_start = offset;
  783. bf.l_len = len;
  784. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  785. if (mode & FALLOC_FL_PUNCH_HOLE)
  786. cmd = XFS_IOC_UNRESVSP;
  787. /* check the new inode size is valid before allocating */
  788. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  789. offset + len > i_size_read(inode)) {
  790. new_size = offset + len;
  791. error = inode_newsize_ok(inode, new_size);
  792. if (error)
  793. goto out_unlock;
  794. }
  795. error = -xfs_change_file_space(ip, cmd, &bf, 0, XFS_ATTR_NOLOCK);
  796. if (error)
  797. goto out_unlock;
  798. /* Change file size if needed */
  799. if (new_size) {
  800. struct iattr iattr;
  801. iattr.ia_valid = ATTR_SIZE;
  802. iattr.ia_size = new_size;
  803. error = -xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK);
  804. }
  805. out_unlock:
  806. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  807. return error;
  808. }
  809. STATIC int
  810. xfs_file_open(
  811. struct inode *inode,
  812. struct file *file)
  813. {
  814. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  815. return -EFBIG;
  816. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  817. return -EIO;
  818. return 0;
  819. }
  820. STATIC int
  821. xfs_dir_open(
  822. struct inode *inode,
  823. struct file *file)
  824. {
  825. struct xfs_inode *ip = XFS_I(inode);
  826. int mode;
  827. int error;
  828. error = xfs_file_open(inode, file);
  829. if (error)
  830. return error;
  831. /*
  832. * If there are any blocks, read-ahead block 0 as we're almost
  833. * certain to have the next operation be a read there.
  834. */
  835. mode = xfs_ilock_map_shared(ip);
  836. if (ip->i_d.di_nextents > 0)
  837. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  838. xfs_iunlock(ip, mode);
  839. return 0;
  840. }
  841. STATIC int
  842. xfs_file_release(
  843. struct inode *inode,
  844. struct file *filp)
  845. {
  846. return -xfs_release(XFS_I(inode));
  847. }
  848. STATIC int
  849. xfs_file_readdir(
  850. struct file *filp,
  851. void *dirent,
  852. filldir_t filldir)
  853. {
  854. struct inode *inode = filp->f_path.dentry->d_inode;
  855. xfs_inode_t *ip = XFS_I(inode);
  856. int error;
  857. size_t bufsize;
  858. /*
  859. * The Linux API doesn't pass down the total size of the buffer
  860. * we read into down to the filesystem. With the filldir concept
  861. * it's not needed for correct information, but the XFS dir2 leaf
  862. * code wants an estimate of the buffer size to calculate it's
  863. * readahead window and size the buffers used for mapping to
  864. * physical blocks.
  865. *
  866. * Try to give it an estimate that's good enough, maybe at some
  867. * point we can change the ->readdir prototype to include the
  868. * buffer size. For now we use the current glibc buffer size.
  869. */
  870. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  871. error = xfs_readdir(ip, dirent, bufsize,
  872. (xfs_off_t *)&filp->f_pos, filldir);
  873. if (error)
  874. return -error;
  875. return 0;
  876. }
  877. STATIC int
  878. xfs_file_mmap(
  879. struct file *filp,
  880. struct vm_area_struct *vma)
  881. {
  882. vma->vm_ops = &xfs_file_vm_ops;
  883. vma->vm_flags |= VM_CAN_NONLINEAR;
  884. file_accessed(filp);
  885. return 0;
  886. }
  887. /*
  888. * mmap()d file has taken write protection fault and is being made
  889. * writable. We can set the page state up correctly for a writable
  890. * page, which means we can do correct delalloc accounting (ENOSPC
  891. * checking!) and unwritten extent mapping.
  892. */
  893. STATIC int
  894. xfs_vm_page_mkwrite(
  895. struct vm_area_struct *vma,
  896. struct vm_fault *vmf)
  897. {
  898. return block_page_mkwrite(vma, vmf, xfs_get_blocks);
  899. }
  900. const struct file_operations xfs_file_operations = {
  901. .llseek = generic_file_llseek,
  902. .read = do_sync_read,
  903. .write = do_sync_write,
  904. .aio_read = xfs_file_aio_read,
  905. .aio_write = xfs_file_aio_write,
  906. .splice_read = xfs_file_splice_read,
  907. .splice_write = xfs_file_splice_write,
  908. .unlocked_ioctl = xfs_file_ioctl,
  909. #ifdef CONFIG_COMPAT
  910. .compat_ioctl = xfs_file_compat_ioctl,
  911. #endif
  912. .mmap = xfs_file_mmap,
  913. .open = xfs_file_open,
  914. .release = xfs_file_release,
  915. .fsync = xfs_file_fsync,
  916. .fallocate = xfs_file_fallocate,
  917. };
  918. const struct file_operations xfs_dir_file_operations = {
  919. .open = xfs_dir_open,
  920. .read = generic_read_dir,
  921. .readdir = xfs_file_readdir,
  922. .llseek = generic_file_llseek,
  923. .unlocked_ioctl = xfs_file_ioctl,
  924. #ifdef CONFIG_COMPAT
  925. .compat_ioctl = xfs_file_compat_ioctl,
  926. #endif
  927. .fsync = xfs_file_fsync,
  928. };
  929. static const struct vm_operations_struct xfs_file_vm_ops = {
  930. .fault = filemap_fault,
  931. .page_mkwrite = xfs_vm_page_mkwrite,
  932. };