xfs_file.c 31 KB

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