xfs_file.c 29 KB

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