xfs_file.c 31 KB

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