xfs_file.c 28 KB

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