xfs_file.c 28 KB

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