xfs_file.c 29 KB

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