xfs_file.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. * xfs_file_dio_aio_write - handle direct IO writes
  549. *
  550. * Lock the inode appropriately to prepare for and issue a direct IO write.
  551. * By spearating it from the buffered write path we remove all the tricky to
  552. * follow locking changes and looping.
  553. *
  554. * Returns with locks held indicated by @iolock and errors indicated by
  555. * negative return values.
  556. */
  557. STATIC ssize_t
  558. xfs_file_dio_aio_write(
  559. struct kiocb *iocb,
  560. const struct iovec *iovp,
  561. unsigned long nr_segs,
  562. loff_t pos,
  563. size_t ocount,
  564. int *iolock)
  565. {
  566. struct file *file = iocb->ki_filp;
  567. struct address_space *mapping = file->f_mapping;
  568. struct inode *inode = mapping->host;
  569. struct xfs_inode *ip = XFS_I(inode);
  570. struct xfs_mount *mp = ip->i_mount;
  571. ssize_t ret = 0;
  572. xfs_fsize_t new_size;
  573. size_t count = ocount;
  574. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  575. mp->m_rtdev_targp : mp->m_ddev_targp;
  576. *iolock = 0;
  577. if ((pos & target->bt_smask) || (count & target->bt_smask))
  578. return -XFS_ERROR(EINVAL);
  579. /*
  580. * For direct I/O, if there are cached pages or we're extending
  581. * the file, we need IOLOCK_EXCL until we're sure the bytes at
  582. * the new EOF have been zeroed and/or the cached pages are
  583. * flushed out.
  584. */
  585. if (mapping->nrpages || pos > ip->i_size)
  586. *iolock = XFS_IOLOCK_EXCL;
  587. else
  588. *iolock = XFS_IOLOCK_SHARED;
  589. xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
  590. ret = generic_write_checks(file, &pos, &count,
  591. S_ISBLK(inode->i_mode));
  592. if (ret) {
  593. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
  594. *iolock = 0;
  595. return ret;
  596. }
  597. new_size = pos + count;
  598. if (new_size > ip->i_size)
  599. ip->i_new_size = new_size;
  600. if (likely(!(file->f_mode & FMODE_NOCMTIME)))
  601. file_update_time(file);
  602. /*
  603. * If the offset is beyond the size of the file, we have a couple of
  604. * things to do. First, if there is already space allocated we need to
  605. * either create holes or zero the disk or ...
  606. *
  607. * If there is a page where the previous size lands, we need to zero it
  608. * out up to the new size.
  609. */
  610. if (pos > ip->i_size) {
  611. ret = -xfs_zero_eof(ip, pos, ip->i_size);
  612. if (ret) {
  613. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  614. return ret;
  615. }
  616. }
  617. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  618. /*
  619. * If we're writing the file then make sure to clear the setuid and
  620. * setgid bits if the process is not being run by root. This keeps
  621. * people from modifying setuid and setgid binaries.
  622. */
  623. ret = file_remove_suid(file);
  624. if (unlikely(ret))
  625. return ret;
  626. if (mapping->nrpages) {
  627. WARN_ON(*iolock != XFS_IOLOCK_EXCL);
  628. ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
  629. FI_REMAPF_LOCKED);
  630. if (ret)
  631. return ret;
  632. }
  633. if (*iolock == XFS_IOLOCK_EXCL) {
  634. /* demote the lock now the cached pages are gone */
  635. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  636. *iolock = XFS_IOLOCK_SHARED;
  637. }
  638. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
  639. ret = generic_file_direct_write(iocb, iovp,
  640. &nr_segs, pos, &iocb->ki_pos, count, ocount);
  641. /* No fallback to buffered IO on errors for XFS. */
  642. ASSERT(ret < 0 || ret == count);
  643. return ret;
  644. }
  645. STATIC ssize_t
  646. xfs_file_buffered_aio_write(
  647. struct kiocb *iocb,
  648. const struct iovec *iovp,
  649. unsigned long nr_segs,
  650. loff_t pos,
  651. size_t ocount,
  652. int *iolock)
  653. {
  654. struct file *file = iocb->ki_filp;
  655. struct address_space *mapping = file->f_mapping;
  656. struct inode *inode = mapping->host;
  657. struct xfs_inode *ip = XFS_I(inode);
  658. ssize_t ret;
  659. int enospc = 0;
  660. xfs_fsize_t new_size;
  661. size_t count = ocount;
  662. *iolock = XFS_IOLOCK_EXCL;
  663. xfs_rw_ilock(ip, XFS_ILOCK_EXCL | *iolock);
  664. ret = generic_write_checks(file, &pos, &count,
  665. S_ISBLK(inode->i_mode));
  666. if (ret) {
  667. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL | *iolock);
  668. *iolock = 0;
  669. return ret;
  670. }
  671. new_size = pos + count;
  672. if (new_size > ip->i_size)
  673. ip->i_new_size = new_size;
  674. if (likely(!(file->f_mode & FMODE_NOCMTIME)))
  675. file_update_time(file);
  676. if (pos > ip->i_size) {
  677. ret = -xfs_zero_eof(ip, pos, ip->i_size);
  678. if (ret) {
  679. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  680. return ret;
  681. }
  682. }
  683. xfs_rw_iunlock(ip, XFS_ILOCK_EXCL);
  684. ret = file_remove_suid(file);
  685. if (unlikely(ret))
  686. return ret;
  687. /* We can write back this queue in page reclaim */
  688. current->backing_dev_info = mapping->backing_dev_info;
  689. write_retry:
  690. trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
  691. ret = generic_file_buffered_write(iocb, iovp, nr_segs,
  692. pos, &iocb->ki_pos, count, ret);
  693. /*
  694. * if we just got an ENOSPC, flush the inode now we aren't holding any
  695. * page locks and retry *once*
  696. */
  697. if (ret == -ENOSPC && !enospc) {
  698. ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
  699. if (ret)
  700. return ret;
  701. enospc = 1;
  702. goto write_retry;
  703. }
  704. current->backing_dev_info = NULL;
  705. return ret;
  706. }
  707. STATIC ssize_t
  708. xfs_file_aio_write(
  709. struct kiocb *iocb,
  710. const struct iovec *iovp,
  711. unsigned long nr_segs,
  712. loff_t pos)
  713. {
  714. struct file *file = iocb->ki_filp;
  715. struct address_space *mapping = file->f_mapping;
  716. struct inode *inode = mapping->host;
  717. struct xfs_inode *ip = XFS_I(inode);
  718. ssize_t ret;
  719. int iolock;
  720. size_t ocount = 0;
  721. XFS_STATS_INC(xs_write_calls);
  722. BUG_ON(iocb->ki_pos != pos);
  723. ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
  724. if (ret)
  725. return ret;
  726. if (ocount == 0)
  727. return 0;
  728. xfs_wait_for_freeze(ip->i_mount, SB_FREEZE_WRITE);
  729. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  730. return -EIO;
  731. if (unlikely(file->f_flags & O_DIRECT))
  732. ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos,
  733. ocount, &iolock);
  734. else
  735. ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
  736. ocount, &iolock);
  737. xfs_aio_write_isize_update(inode, &iocb->ki_pos, ret);
  738. if (ret <= 0)
  739. goto out_unlock;
  740. /* Handle various SYNC-type writes */
  741. if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
  742. loff_t end = pos + ret - 1;
  743. int error, error2;
  744. xfs_rw_iunlock(ip, iolock);
  745. error = filemap_write_and_wait_range(mapping, pos, end);
  746. xfs_rw_ilock(ip, iolock);
  747. error2 = -xfs_file_fsync(file,
  748. (file->f_flags & __O_SYNC) ? 0 : 1);
  749. if (error)
  750. ret = error;
  751. else if (error2)
  752. ret = error2;
  753. }
  754. out_unlock:
  755. xfs_aio_write_newsize_update(ip);
  756. xfs_rw_iunlock(ip, iolock);
  757. return ret;
  758. }
  759. STATIC int
  760. xfs_file_open(
  761. struct inode *inode,
  762. struct file *file)
  763. {
  764. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  765. return -EFBIG;
  766. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  767. return -EIO;
  768. return 0;
  769. }
  770. STATIC int
  771. xfs_dir_open(
  772. struct inode *inode,
  773. struct file *file)
  774. {
  775. struct xfs_inode *ip = XFS_I(inode);
  776. int mode;
  777. int error;
  778. error = xfs_file_open(inode, file);
  779. if (error)
  780. return error;
  781. /*
  782. * If there are any blocks, read-ahead block 0 as we're almost
  783. * certain to have the next operation be a read there.
  784. */
  785. mode = xfs_ilock_map_shared(ip);
  786. if (ip->i_d.di_nextents > 0)
  787. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  788. xfs_iunlock(ip, mode);
  789. return 0;
  790. }
  791. STATIC int
  792. xfs_file_release(
  793. struct inode *inode,
  794. struct file *filp)
  795. {
  796. return -xfs_release(XFS_I(inode));
  797. }
  798. STATIC int
  799. xfs_file_readdir(
  800. struct file *filp,
  801. void *dirent,
  802. filldir_t filldir)
  803. {
  804. struct inode *inode = filp->f_path.dentry->d_inode;
  805. xfs_inode_t *ip = XFS_I(inode);
  806. int error;
  807. size_t bufsize;
  808. /*
  809. * The Linux API doesn't pass down the total size of the buffer
  810. * we read into down to the filesystem. With the filldir concept
  811. * it's not needed for correct information, but the XFS dir2 leaf
  812. * code wants an estimate of the buffer size to calculate it's
  813. * readahead window and size the buffers used for mapping to
  814. * physical blocks.
  815. *
  816. * Try to give it an estimate that's good enough, maybe at some
  817. * point we can change the ->readdir prototype to include the
  818. * buffer size. For now we use the current glibc buffer size.
  819. */
  820. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  821. error = xfs_readdir(ip, dirent, bufsize,
  822. (xfs_off_t *)&filp->f_pos, filldir);
  823. if (error)
  824. return -error;
  825. return 0;
  826. }
  827. STATIC int
  828. xfs_file_mmap(
  829. struct file *filp,
  830. struct vm_area_struct *vma)
  831. {
  832. vma->vm_ops = &xfs_file_vm_ops;
  833. vma->vm_flags |= VM_CAN_NONLINEAR;
  834. file_accessed(filp);
  835. return 0;
  836. }
  837. /*
  838. * mmap()d file has taken write protection fault and is being made
  839. * writable. We can set the page state up correctly for a writable
  840. * page, which means we can do correct delalloc accounting (ENOSPC
  841. * checking!) and unwritten extent mapping.
  842. */
  843. STATIC int
  844. xfs_vm_page_mkwrite(
  845. struct vm_area_struct *vma,
  846. struct vm_fault *vmf)
  847. {
  848. return block_page_mkwrite(vma, vmf, xfs_get_blocks);
  849. }
  850. const struct file_operations xfs_file_operations = {
  851. .llseek = generic_file_llseek,
  852. .read = do_sync_read,
  853. .write = do_sync_write,
  854. .aio_read = xfs_file_aio_read,
  855. .aio_write = xfs_file_aio_write,
  856. .splice_read = xfs_file_splice_read,
  857. .splice_write = xfs_file_splice_write,
  858. .unlocked_ioctl = xfs_file_ioctl,
  859. #ifdef CONFIG_COMPAT
  860. .compat_ioctl = xfs_file_compat_ioctl,
  861. #endif
  862. .mmap = xfs_file_mmap,
  863. .open = xfs_file_open,
  864. .release = xfs_file_release,
  865. .fsync = xfs_file_fsync,
  866. };
  867. const struct file_operations xfs_dir_file_operations = {
  868. .open = xfs_dir_open,
  869. .read = generic_read_dir,
  870. .readdir = xfs_file_readdir,
  871. .llseek = generic_file_llseek,
  872. .unlocked_ioctl = xfs_file_ioctl,
  873. #ifdef CONFIG_COMPAT
  874. .compat_ioctl = xfs_file_compat_ioctl,
  875. #endif
  876. .fsync = xfs_file_fsync,
  877. };
  878. static const struct vm_operations_struct xfs_file_vm_ops = {
  879. .fault = filemap_fault,
  880. .page_mkwrite = xfs_vm_page_mkwrite,
  881. };