xfs_file.c 26 KB

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