xfs_file.c 26 KB

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