xfs_file.c 31 KB

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