xfs_file.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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_log.h"
  21. #include "xfs_sb.h"
  22. #include "xfs_ag.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_bmap_btree.h"
  26. #include "xfs_alloc.h"
  27. #include "xfs_dinode.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_inode_item.h"
  30. #include "xfs_bmap.h"
  31. #include "xfs_error.h"
  32. #include "xfs_vnodeops.h"
  33. #include "xfs_da_btree.h"
  34. #include "xfs_ioctl.h"
  35. #include "xfs_trace.h"
  36. #include <linux/dcache.h>
  37. #include <linux/falloc.h>
  38. static const struct vm_operations_struct xfs_file_vm_ops;
  39. /*
  40. * Locking primitives for read and write IO paths to ensure we consistently use
  41. * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
  42. */
  43. static inline void
  44. xfs_rw_ilock(
  45. struct xfs_inode *ip,
  46. int type)
  47. {
  48. if (type & XFS_IOLOCK_EXCL)
  49. mutex_lock(&VFS_I(ip)->i_mutex);
  50. xfs_ilock(ip, type);
  51. }
  52. static inline void
  53. xfs_rw_iunlock(
  54. struct xfs_inode *ip,
  55. int type)
  56. {
  57. xfs_iunlock(ip, type);
  58. if (type & XFS_IOLOCK_EXCL)
  59. mutex_unlock(&VFS_I(ip)->i_mutex);
  60. }
  61. static inline void
  62. xfs_rw_ilock_demote(
  63. struct xfs_inode *ip,
  64. int type)
  65. {
  66. xfs_ilock_demote(ip, type);
  67. if (type & XFS_IOLOCK_EXCL)
  68. mutex_unlock(&VFS_I(ip)->i_mutex);
  69. }
  70. /*
  71. * xfs_iozero
  72. *
  73. * xfs_iozero clears the specified range of buffer supplied,
  74. * and marks all the affected blocks as valid and modified. If
  75. * an affected block is not allocated, it will be allocated. If
  76. * an affected block is not completely overwritten, and is not
  77. * valid before the operation, it will be read from disk before
  78. * being partially zeroed.
  79. */
  80. STATIC int
  81. xfs_iozero(
  82. struct xfs_inode *ip, /* inode */
  83. loff_t pos, /* offset in file */
  84. size_t count) /* size of data to zero */
  85. {
  86. struct page *page;
  87. struct address_space *mapping;
  88. int status;
  89. mapping = VFS_I(ip)->i_mapping;
  90. do {
  91. unsigned offset, bytes;
  92. void *fsdata;
  93. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  94. bytes = PAGE_CACHE_SIZE - offset;
  95. if (bytes > count)
  96. bytes = count;
  97. status = pagecache_write_begin(NULL, mapping, pos, bytes,
  98. AOP_FLAG_UNINTERRUPTIBLE,
  99. &page, &fsdata);
  100. if (status)
  101. break;
  102. zero_user(page, offset, bytes);
  103. status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
  104. page, fsdata);
  105. WARN_ON(status <= 0); /* can't return less than zero! */
  106. pos += bytes;
  107. count -= bytes;
  108. status = 0;
  109. } while (count);
  110. return (-status);
  111. }
  112. /*
  113. * Fsync operations on directories are much simpler than on regular files,
  114. * as there is no file data to flush, and thus also no need for explicit
  115. * cache flush operations, and there are no non-transaction metadata updates
  116. * on directories either.
  117. */
  118. STATIC int
  119. xfs_dir_fsync(
  120. struct file *file,
  121. loff_t start,
  122. loff_t end,
  123. int datasync)
  124. {
  125. struct xfs_inode *ip = XFS_I(file->f_mapping->host);
  126. struct xfs_mount *mp = ip->i_mount;
  127. xfs_lsn_t lsn = 0;
  128. trace_xfs_dir_fsync(ip);
  129. xfs_ilock(ip, XFS_ILOCK_SHARED);
  130. if (xfs_ipincount(ip))
  131. lsn = ip->i_itemp->ili_last_lsn;
  132. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  133. if (!lsn)
  134. return 0;
  135. return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
  136. }
  137. STATIC int
  138. xfs_file_fsync(
  139. struct file *file,
  140. loff_t start,
  141. loff_t end,
  142. int datasync)
  143. {
  144. struct inode *inode = file->f_mapping->host;
  145. struct xfs_inode *ip = XFS_I(inode);
  146. struct xfs_mount *mp = ip->i_mount;
  147. int error = 0;
  148. int log_flushed = 0;
  149. xfs_lsn_t lsn = 0;
  150. trace_xfs_file_fsync(ip);
  151. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  152. if (error)
  153. return error;
  154. if (XFS_FORCED_SHUTDOWN(mp))
  155. return -XFS_ERROR(EIO);
  156. xfs_iflags_clear(ip, XFS_ITRUNCATED);
  157. if (mp->m_flags & XFS_MOUNT_BARRIER) {
  158. /*
  159. * If we have an RT and/or log subvolume we need to make sure
  160. * to flush the write cache the device used for file data
  161. * first. This is to ensure newly written file data make
  162. * it to disk before logging the new inode size in case of
  163. * an extending write.
  164. */
  165. if (XFS_IS_REALTIME_INODE(ip))
  166. xfs_blkdev_issue_flush(mp->m_rtdev_targp);
  167. else if (mp->m_logdev_targp != mp->m_ddev_targp)
  168. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  169. }
  170. /*
  171. * All metadata updates are logged, which means that we just have
  172. * to flush the log up to the latest LSN that touched the inode.
  173. */
  174. xfs_ilock(ip, XFS_ILOCK_SHARED);
  175. if (xfs_ipincount(ip)) {
  176. if (!datasync ||
  177. (ip->i_itemp->ili_fields & ~XFS_ILOG_TIMESTAMP))
  178. lsn = ip->i_itemp->ili_last_lsn;
  179. }
  180. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  181. if (lsn)
  182. error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
  183. /*
  184. * If we only have a single device, and the log force about was
  185. * a no-op we might have to flush the data device cache here.
  186. * This can only happen for fdatasync/O_DSYNC if we were overwriting
  187. * an already allocated file and thus do not have any metadata to
  188. * commit.
  189. */
  190. if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
  191. mp->m_logdev_targp == mp->m_ddev_targp &&
  192. !XFS_IS_REALTIME_INODE(ip) &&
  193. !log_flushed)
  194. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  195. return -error;
  196. }
  197. STATIC ssize_t
  198. xfs_file_aio_read(
  199. struct kiocb *iocb,
  200. const struct iovec *iovp,
  201. unsigned long nr_segs,
  202. loff_t pos)
  203. {
  204. struct file *file = iocb->ki_filp;
  205. struct inode *inode = file->f_mapping->host;
  206. struct xfs_inode *ip = XFS_I(inode);
  207. struct xfs_mount *mp = ip->i_mount;
  208. size_t size = 0;
  209. ssize_t ret = 0;
  210. int ioflags = 0;
  211. xfs_fsize_t n;
  212. unsigned long seg;
  213. XFS_STATS_INC(xs_read_calls);
  214. BUG_ON(iocb->ki_pos != pos);
  215. if (unlikely(file->f_flags & O_DIRECT))
  216. ioflags |= IO_ISDIRECT;
  217. if (file->f_mode & FMODE_NOCMTIME)
  218. ioflags |= IO_INVIS;
  219. /* START copy & waste from filemap.c */
  220. for (seg = 0; seg < nr_segs; seg++) {
  221. const struct iovec *iv = &iovp[seg];
  222. /*
  223. * If any segment has a negative length, or the cumulative
  224. * length ever wraps negative then return -EINVAL.
  225. */
  226. size += iv->iov_len;
  227. if (unlikely((ssize_t)(size|iv->iov_len) < 0))
  228. return XFS_ERROR(-EINVAL);
  229. }
  230. /* END copy & waste from filemap.c */
  231. if (unlikely(ioflags & IO_ISDIRECT)) {
  232. xfs_buftarg_t *target =
  233. XFS_IS_REALTIME_INODE(ip) ?
  234. mp->m_rtdev_targp : mp->m_ddev_targp;
  235. if ((iocb->ki_pos & target->bt_smask) ||
  236. (size & target->bt_smask)) {
  237. if (iocb->ki_pos == i_size_read(inode))
  238. return 0;
  239. return -XFS_ERROR(EINVAL);
  240. }
  241. }
  242. n = XFS_MAXIOFFSET(mp) - iocb->ki_pos;
  243. if (n <= 0 || size == 0)
  244. return 0;
  245. if (n < size)
  246. size = n;
  247. if (XFS_FORCED_SHUTDOWN(mp))
  248. return -EIO;
  249. /*
  250. * Locking is a bit tricky here. If we take an exclusive lock
  251. * for direct IO, we effectively serialise all new concurrent
  252. * read IO to this file and block it behind IO that is currently in
  253. * progress because IO in progress holds the IO lock shared. We only
  254. * need to hold the lock exclusive to blow away the page cache, so
  255. * only take lock exclusively if the page cache needs invalidation.
  256. * This allows the normal direct IO case of no page cache pages to
  257. * proceeed concurrently without serialisation.
  258. */
  259. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  260. if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
  261. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  262. xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
  263. if (inode->i_mapping->nrpages) {
  264. ret = -xfs_flushinval_pages(ip,
  265. (iocb->ki_pos & PAGE_CACHE_MASK),
  266. -1, FI_REMAPF_LOCKED);
  267. if (ret) {
  268. xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
  269. return ret;
  270. }
  271. }
  272. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  273. }
  274. trace_xfs_file_read(ip, size, iocb->ki_pos, ioflags);
  275. ret = generic_file_aio_read(iocb, iovp, nr_segs, iocb->ki_pos);
  276. if (ret > 0)
  277. XFS_STATS_ADD(xs_read_bytes, ret);
  278. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  279. return ret;
  280. }
  281. STATIC ssize_t
  282. xfs_file_splice_read(
  283. struct file *infilp,
  284. loff_t *ppos,
  285. struct pipe_inode_info *pipe,
  286. size_t count,
  287. unsigned int flags)
  288. {
  289. struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
  290. int ioflags = 0;
  291. ssize_t ret;
  292. XFS_STATS_INC(xs_read_calls);
  293. if (infilp->f_mode & FMODE_NOCMTIME)
  294. ioflags |= IO_INVIS;
  295. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  296. return -EIO;
  297. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  298. trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
  299. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  300. if (ret > 0)
  301. XFS_STATS_ADD(xs_read_bytes, ret);
  302. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  303. return ret;
  304. }
  305. /*
  306. * xfs_file_splice_write() does not use xfs_rw_ilock() because
  307. * generic_file_splice_write() takes the i_mutex itself. This, in theory,
  308. * couuld cause lock inversions between the aio_write path and the splice path
  309. * if someone is doing concurrent splice(2) based writes and write(2) based
  310. * writes to the same inode. The only real way to fix this is to re-implement
  311. * the generic code here with correct locking orders.
  312. */
  313. STATIC ssize_t
  314. xfs_file_splice_write(
  315. struct pipe_inode_info *pipe,
  316. struct file *outfilp,
  317. loff_t *ppos,
  318. size_t count,
  319. unsigned int flags)
  320. {
  321. struct inode *inode = outfilp->f_mapping->host;
  322. struct xfs_inode *ip = XFS_I(inode);
  323. int ioflags = 0;
  324. ssize_t ret;
  325. XFS_STATS_INC(xs_write_calls);
  326. if (outfilp->f_mode & FMODE_NOCMTIME)
  327. ioflags |= IO_INVIS;
  328. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  329. return -EIO;
  330. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  331. trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
  332. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  333. if (ret > 0)
  334. XFS_STATS_ADD(xs_write_bytes, ret);
  335. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  336. return ret;
  337. }
  338. /*
  339. * This routine is called to handle zeroing any space in the last block of the
  340. * file that is beyond the EOF. We do this since the size is being increased
  341. * without writing anything to that block and we don't want to read the
  342. * garbage on the disk.
  343. */
  344. STATIC int /* error (positive) */
  345. xfs_zero_last_block(
  346. struct xfs_inode *ip,
  347. xfs_fsize_t offset,
  348. xfs_fsize_t isize)
  349. {
  350. struct xfs_mount *mp = ip->i_mount;
  351. xfs_fileoff_t last_fsb = XFS_B_TO_FSBT(mp, isize);
  352. int zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  353. int zero_len;
  354. int nimaps = 1;
  355. int error = 0;
  356. struct xfs_bmbt_irec imap;
  357. xfs_ilock(ip, XFS_ILOCK_EXCL);
  358. error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
  359. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  360. if (error)
  361. return error;
  362. ASSERT(nimaps > 0);
  363. /*
  364. * If the block underlying isize is just a hole, then there
  365. * is nothing to zero.
  366. */
  367. if (imap.br_startblock == HOLESTARTBLOCK)
  368. return 0;
  369. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  370. if (isize + zero_len > offset)
  371. zero_len = offset - isize;
  372. return xfs_iozero(ip, isize, zero_len);
  373. }
  374. /*
  375. * Zero any on disk space between the current EOF and the new, larger EOF.
  376. *
  377. * This handles the normal case of zeroing the remainder of the last block in
  378. * the file and the unusual case of zeroing blocks out beyond the size of the
  379. * file. This second case only happens with fixed size extents and when the
  380. * system crashes before the inode size was updated but after blocks were
  381. * allocated.
  382. *
  383. * Expects the iolock to be held exclusive, and will take the ilock internally.
  384. */
  385. int /* error (positive) */
  386. xfs_zero_eof(
  387. struct xfs_inode *ip,
  388. xfs_off_t offset, /* starting I/O offset */
  389. xfs_fsize_t isize) /* current inode size */
  390. {
  391. struct xfs_mount *mp = ip->i_mount;
  392. xfs_fileoff_t start_zero_fsb;
  393. xfs_fileoff_t end_zero_fsb;
  394. xfs_fileoff_t zero_count_fsb;
  395. xfs_fileoff_t last_fsb;
  396. xfs_fileoff_t zero_off;
  397. xfs_fsize_t zero_len;
  398. int nimaps;
  399. int error = 0;
  400. struct xfs_bmbt_irec imap;
  401. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  402. ASSERT(offset > isize);
  403. /*
  404. * First handle zeroing the block on which isize resides.
  405. *
  406. * We only zero a part of that block so it is handled specially.
  407. */
  408. if (XFS_B_FSB_OFFSET(mp, isize) != 0) {
  409. error = xfs_zero_last_block(ip, offset, isize);
  410. if (error)
  411. return error;
  412. }
  413. /*
  414. * Calculate the range between the new size and the old where blocks
  415. * needing to be zeroed may exist.
  416. *
  417. * To get the block where the last byte in the file currently resides,
  418. * we need to subtract one from the size and truncate back to a block
  419. * boundary. We subtract 1 in case the size is exactly on a block
  420. * boundary.
  421. */
  422. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  423. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  424. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  425. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  426. if (last_fsb == end_zero_fsb) {
  427. /*
  428. * The size was only incremented on its last block.
  429. * We took care of that above, so just return.
  430. */
  431. return 0;
  432. }
  433. ASSERT(start_zero_fsb <= end_zero_fsb);
  434. while (start_zero_fsb <= end_zero_fsb) {
  435. nimaps = 1;
  436. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  437. xfs_ilock(ip, XFS_ILOCK_EXCL);
  438. error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
  439. &imap, &nimaps, 0);
  440. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  441. if (error)
  442. return error;
  443. ASSERT(nimaps > 0);
  444. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  445. imap.br_startblock == HOLESTARTBLOCK) {
  446. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  447. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  448. continue;
  449. }
  450. /*
  451. * There are blocks we need to zero.
  452. */
  453. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  454. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  455. if ((zero_off + zero_len) > offset)
  456. zero_len = offset - zero_off;
  457. error = xfs_iozero(ip, zero_off, zero_len);
  458. if (error)
  459. return error;
  460. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  461. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  462. }
  463. return 0;
  464. }
  465. /*
  466. * Common pre-write limit and setup checks.
  467. *
  468. * Called with the iolocked held either shared and exclusive according to
  469. * @iolock, and returns with it held. Might upgrade the iolock to exclusive
  470. * if called for a direct write beyond i_size.
  471. */
  472. STATIC ssize_t
  473. xfs_file_aio_write_checks(
  474. struct file *file,
  475. loff_t *pos,
  476. size_t *count,
  477. int *iolock)
  478. {
  479. struct inode *inode = file->f_mapping->host;
  480. struct xfs_inode *ip = XFS_I(inode);
  481. int error = 0;
  482. restart:
  483. error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
  484. if (error)
  485. return error;
  486. /*
  487. * If the offset is beyond the size of the file, we need to zero any
  488. * blocks that fall between the existing EOF and the start of this
  489. * write. If zeroing is needed and we are currently holding the
  490. * iolock shared, we need to update it to exclusive which implies
  491. * having to redo all checks before.
  492. */
  493. if (*pos > i_size_read(inode)) {
  494. if (*iolock == XFS_IOLOCK_SHARED) {
  495. xfs_rw_iunlock(ip, *iolock);
  496. *iolock = XFS_IOLOCK_EXCL;
  497. xfs_rw_ilock(ip, *iolock);
  498. goto restart;
  499. }
  500. error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
  501. if (error)
  502. return error;
  503. }
  504. /*
  505. * Updating the timestamps will grab the ilock again from
  506. * xfs_fs_dirty_inode, so we have to call it after dropping the
  507. * lock above. Eventually we should look into a way to avoid
  508. * the pointless lock roundtrip.
  509. */
  510. if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
  511. error = file_update_time(file);
  512. if (error)
  513. return error;
  514. }
  515. /*
  516. * If we're writing the file then make sure to clear the setuid and
  517. * setgid bits if the process is not being run by root. This keeps
  518. * people from modifying setuid and setgid binaries.
  519. */
  520. return file_remove_suid(file);
  521. }
  522. /*
  523. * xfs_file_dio_aio_write - handle direct IO writes
  524. *
  525. * Lock the inode appropriately to prepare for and issue a direct IO write.
  526. * By separating it from the buffered write path we remove all the tricky to
  527. * follow locking changes and looping.
  528. *
  529. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  530. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  531. * pages are flushed out.
  532. *
  533. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  534. * allowing them to be done in parallel with reads and other direct IO writes.
  535. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  536. * needs to do sub-block zeroing and that requires serialisation against other
  537. * direct IOs to the same block. In this case we need to serialise the
  538. * submission of the unaligned IOs so that we don't get racing block zeroing in
  539. * the dio layer. To avoid the problem with aio, we also need to wait for
  540. * outstanding IOs to complete so that unwritten extent conversion is completed
  541. * before we try to map the overlapping block. This is currently implemented by
  542. * hitting it with a big hammer (i.e. inode_dio_wait()).
  543. *
  544. * Returns with locks held indicated by @iolock and errors indicated by
  545. * negative return values.
  546. */
  547. STATIC ssize_t
  548. xfs_file_dio_aio_write(
  549. struct kiocb *iocb,
  550. const struct iovec *iovp,
  551. unsigned long nr_segs,
  552. loff_t pos,
  553. size_t ocount)
  554. {
  555. struct file *file = iocb->ki_filp;
  556. struct address_space *mapping = file->f_mapping;
  557. struct inode *inode = mapping->host;
  558. struct xfs_inode *ip = XFS_I(inode);
  559. struct xfs_mount *mp = ip->i_mount;
  560. ssize_t ret = 0;
  561. size_t count = ocount;
  562. int unaligned_io = 0;
  563. int iolock;
  564. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  565. mp->m_rtdev_targp : mp->m_ddev_targp;
  566. if ((pos & target->bt_smask) || (count & target->bt_smask))
  567. return -XFS_ERROR(EINVAL);
  568. if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
  569. unaligned_io = 1;
  570. /*
  571. * We don't need to take an exclusive lock unless there page cache needs
  572. * to be invalidated or unaligned IO is being executed. We don't need to
  573. * consider the EOF extension case here because
  574. * xfs_file_aio_write_checks() will relock the inode as necessary for
  575. * EOF zeroing cases and fill out the new inode size as appropriate.
  576. */
  577. if (unaligned_io || mapping->nrpages)
  578. iolock = XFS_IOLOCK_EXCL;
  579. else
  580. iolock = XFS_IOLOCK_SHARED;
  581. xfs_rw_ilock(ip, iolock);
  582. /*
  583. * Recheck if there are cached pages that need invalidate after we got
  584. * the iolock to protect against other threads adding new pages while
  585. * we were waiting for the iolock.
  586. */
  587. if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
  588. xfs_rw_iunlock(ip, iolock);
  589. iolock = XFS_IOLOCK_EXCL;
  590. xfs_rw_ilock(ip, iolock);
  591. }
  592. ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
  593. if (ret)
  594. goto out;
  595. if (mapping->nrpages) {
  596. ret = -xfs_flushinval_pages(ip, (pos & PAGE_CACHE_MASK), -1,
  597. FI_REMAPF_LOCKED);
  598. if (ret)
  599. goto out;
  600. }
  601. /*
  602. * If we are doing unaligned IO, wait for all other IO to drain,
  603. * otherwise demote the lock if we had to flush cached pages
  604. */
  605. if (unaligned_io)
  606. inode_dio_wait(inode);
  607. else if (iolock == XFS_IOLOCK_EXCL) {
  608. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  609. iolock = XFS_IOLOCK_SHARED;
  610. }
  611. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
  612. ret = generic_file_direct_write(iocb, iovp,
  613. &nr_segs, pos, &iocb->ki_pos, count, ocount);
  614. out:
  615. xfs_rw_iunlock(ip, iolock);
  616. /* No fallback to buffered IO on errors for XFS. */
  617. ASSERT(ret < 0 || ret == count);
  618. return ret;
  619. }
  620. STATIC ssize_t
  621. xfs_file_buffered_aio_write(
  622. struct kiocb *iocb,
  623. const struct iovec *iovp,
  624. unsigned long nr_segs,
  625. loff_t pos,
  626. size_t ocount)
  627. {
  628. struct file *file = iocb->ki_filp;
  629. struct address_space *mapping = file->f_mapping;
  630. struct inode *inode = mapping->host;
  631. struct xfs_inode *ip = XFS_I(inode);
  632. ssize_t ret;
  633. int enospc = 0;
  634. int iolock = XFS_IOLOCK_EXCL;
  635. size_t count = ocount;
  636. xfs_rw_ilock(ip, iolock);
  637. ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
  638. if (ret)
  639. goto out;
  640. /* We can write back this queue in page reclaim */
  641. current->backing_dev_info = mapping->backing_dev_info;
  642. write_retry:
  643. trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
  644. ret = generic_file_buffered_write(iocb, iovp, nr_segs,
  645. pos, &iocb->ki_pos, count, ret);
  646. /*
  647. * if we just got an ENOSPC, flush the inode now we aren't holding any
  648. * page locks and retry *once*
  649. */
  650. if (ret == -ENOSPC && !enospc) {
  651. enospc = 1;
  652. ret = -xfs_flush_pages(ip, 0, -1, 0, FI_NONE);
  653. if (!ret)
  654. goto write_retry;
  655. }
  656. current->backing_dev_info = NULL;
  657. out:
  658. xfs_rw_iunlock(ip, iolock);
  659. return ret;
  660. }
  661. STATIC ssize_t
  662. xfs_file_aio_write(
  663. struct kiocb *iocb,
  664. const struct iovec *iovp,
  665. unsigned long nr_segs,
  666. loff_t pos)
  667. {
  668. struct file *file = iocb->ki_filp;
  669. struct address_space *mapping = file->f_mapping;
  670. struct inode *inode = mapping->host;
  671. struct xfs_inode *ip = XFS_I(inode);
  672. ssize_t ret;
  673. size_t ocount = 0;
  674. XFS_STATS_INC(xs_write_calls);
  675. BUG_ON(iocb->ki_pos != pos);
  676. ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
  677. if (ret)
  678. return ret;
  679. if (ocount == 0)
  680. return 0;
  681. sb_start_write(inode->i_sb);
  682. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  683. ret = -EIO;
  684. goto out;
  685. }
  686. if (unlikely(file->f_flags & O_DIRECT))
  687. ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, ocount);
  688. else
  689. ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
  690. ocount);
  691. if (ret > 0) {
  692. ssize_t err;
  693. XFS_STATS_ADD(xs_write_bytes, ret);
  694. /* Handle various SYNC-type writes */
  695. err = generic_write_sync(file, pos, ret);
  696. if (err < 0)
  697. ret = err;
  698. }
  699. out:
  700. sb_end_write(inode->i_sb);
  701. return ret;
  702. }
  703. STATIC long
  704. xfs_file_fallocate(
  705. struct file *file,
  706. int mode,
  707. loff_t offset,
  708. loff_t len)
  709. {
  710. struct inode *inode = file->f_path.dentry->d_inode;
  711. long error;
  712. loff_t new_size = 0;
  713. xfs_flock64_t bf;
  714. xfs_inode_t *ip = XFS_I(inode);
  715. int cmd = XFS_IOC_RESVSP;
  716. int attr_flags = XFS_ATTR_NOLOCK;
  717. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  718. return -EOPNOTSUPP;
  719. bf.l_whence = 0;
  720. bf.l_start = offset;
  721. bf.l_len = len;
  722. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  723. if (mode & FALLOC_FL_PUNCH_HOLE)
  724. cmd = XFS_IOC_UNRESVSP;
  725. /* check the new inode size is valid before allocating */
  726. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  727. offset + len > i_size_read(inode)) {
  728. new_size = offset + len;
  729. error = inode_newsize_ok(inode, new_size);
  730. if (error)
  731. goto out_unlock;
  732. }
  733. if (file->f_flags & O_DSYNC)
  734. attr_flags |= XFS_ATTR_SYNC;
  735. error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
  736. if (error)
  737. goto out_unlock;
  738. /* Change file size if needed */
  739. if (new_size) {
  740. struct iattr iattr;
  741. iattr.ia_valid = ATTR_SIZE;
  742. iattr.ia_size = new_size;
  743. error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
  744. }
  745. out_unlock:
  746. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  747. return error;
  748. }
  749. STATIC int
  750. xfs_file_open(
  751. struct inode *inode,
  752. struct file *file)
  753. {
  754. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  755. return -EFBIG;
  756. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  757. return -EIO;
  758. return 0;
  759. }
  760. STATIC int
  761. xfs_dir_open(
  762. struct inode *inode,
  763. struct file *file)
  764. {
  765. struct xfs_inode *ip = XFS_I(inode);
  766. int mode;
  767. int error;
  768. error = xfs_file_open(inode, file);
  769. if (error)
  770. return error;
  771. /*
  772. * If there are any blocks, read-ahead block 0 as we're almost
  773. * certain to have the next operation be a read there.
  774. */
  775. mode = xfs_ilock_map_shared(ip);
  776. if (ip->i_d.di_nextents > 0)
  777. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  778. xfs_iunlock(ip, mode);
  779. return 0;
  780. }
  781. STATIC int
  782. xfs_file_release(
  783. struct inode *inode,
  784. struct file *filp)
  785. {
  786. return -xfs_release(XFS_I(inode));
  787. }
  788. STATIC int
  789. xfs_file_readdir(
  790. struct file *filp,
  791. void *dirent,
  792. filldir_t filldir)
  793. {
  794. struct inode *inode = filp->f_path.dentry->d_inode;
  795. xfs_inode_t *ip = XFS_I(inode);
  796. int error;
  797. size_t bufsize;
  798. /*
  799. * The Linux API doesn't pass down the total size of the buffer
  800. * we read into down to the filesystem. With the filldir concept
  801. * it's not needed for correct information, but the XFS dir2 leaf
  802. * code wants an estimate of the buffer size to calculate it's
  803. * readahead window and size the buffers used for mapping to
  804. * physical blocks.
  805. *
  806. * Try to give it an estimate that's good enough, maybe at some
  807. * point we can change the ->readdir prototype to include the
  808. * buffer size. For now we use the current glibc buffer size.
  809. */
  810. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  811. error = xfs_readdir(ip, dirent, bufsize,
  812. (xfs_off_t *)&filp->f_pos, filldir);
  813. if (error)
  814. return -error;
  815. return 0;
  816. }
  817. STATIC int
  818. xfs_file_mmap(
  819. struct file *filp,
  820. struct vm_area_struct *vma)
  821. {
  822. vma->vm_ops = &xfs_file_vm_ops;
  823. vma->vm_flags |= VM_CAN_NONLINEAR;
  824. file_accessed(filp);
  825. return 0;
  826. }
  827. /*
  828. * mmap()d file has taken write protection fault and is being made
  829. * writable. We can set the page state up correctly for a writable
  830. * page, which means we can do correct delalloc accounting (ENOSPC
  831. * checking!) and unwritten extent mapping.
  832. */
  833. STATIC int
  834. xfs_vm_page_mkwrite(
  835. struct vm_area_struct *vma,
  836. struct vm_fault *vmf)
  837. {
  838. return block_page_mkwrite(vma, vmf, xfs_get_blocks);
  839. }
  840. STATIC loff_t
  841. xfs_seek_data(
  842. struct file *file,
  843. loff_t start,
  844. u32 type)
  845. {
  846. struct inode *inode = file->f_mapping->host;
  847. struct xfs_inode *ip = XFS_I(inode);
  848. struct xfs_mount *mp = ip->i_mount;
  849. struct xfs_bmbt_irec map[2];
  850. int nmap = 2;
  851. loff_t uninitialized_var(offset);
  852. xfs_fsize_t isize;
  853. xfs_fileoff_t fsbno;
  854. xfs_filblks_t end;
  855. uint lock;
  856. int error;
  857. lock = xfs_ilock_map_shared(ip);
  858. isize = i_size_read(inode);
  859. if (start >= isize) {
  860. error = ENXIO;
  861. goto out_unlock;
  862. }
  863. fsbno = XFS_B_TO_FSBT(mp, start);
  864. /*
  865. * Try to read extents from the first block indicated
  866. * by fsbno to the end block of the file.
  867. */
  868. end = XFS_B_TO_FSB(mp, isize);
  869. error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
  870. XFS_BMAPI_ENTIRE);
  871. if (error)
  872. goto out_unlock;
  873. /*
  874. * Treat unwritten extent as data extent since it might
  875. * contains dirty data in page cache.
  876. */
  877. if (map[0].br_startblock != HOLESTARTBLOCK) {
  878. offset = max_t(loff_t, start,
  879. XFS_FSB_TO_B(mp, map[0].br_startoff));
  880. } else {
  881. if (nmap == 1) {
  882. error = ENXIO;
  883. goto out_unlock;
  884. }
  885. offset = max_t(loff_t, start,
  886. XFS_FSB_TO_B(mp, map[1].br_startoff));
  887. }
  888. if (offset != file->f_pos)
  889. file->f_pos = offset;
  890. out_unlock:
  891. xfs_iunlock_map_shared(ip, lock);
  892. if (error)
  893. return -error;
  894. return offset;
  895. }
  896. STATIC loff_t
  897. xfs_seek_hole(
  898. struct file *file,
  899. loff_t start,
  900. u32 type)
  901. {
  902. struct inode *inode = file->f_mapping->host;
  903. struct xfs_inode *ip = XFS_I(inode);
  904. struct xfs_mount *mp = ip->i_mount;
  905. loff_t uninitialized_var(offset);
  906. loff_t holeoff;
  907. xfs_fsize_t isize;
  908. xfs_fileoff_t fsbno;
  909. uint lock;
  910. int error;
  911. if (XFS_FORCED_SHUTDOWN(mp))
  912. return -XFS_ERROR(EIO);
  913. lock = xfs_ilock_map_shared(ip);
  914. isize = i_size_read(inode);
  915. if (start >= isize) {
  916. error = ENXIO;
  917. goto out_unlock;
  918. }
  919. fsbno = XFS_B_TO_FSBT(mp, start);
  920. error = xfs_bmap_first_unused(NULL, ip, 1, &fsbno, XFS_DATA_FORK);
  921. if (error)
  922. goto out_unlock;
  923. holeoff = XFS_FSB_TO_B(mp, fsbno);
  924. if (holeoff <= start)
  925. offset = start;
  926. else {
  927. /*
  928. * xfs_bmap_first_unused() could return a value bigger than
  929. * isize if there are no more holes past the supplied offset.
  930. */
  931. offset = min_t(loff_t, holeoff, isize);
  932. }
  933. if (offset != file->f_pos)
  934. file->f_pos = offset;
  935. out_unlock:
  936. xfs_iunlock_map_shared(ip, lock);
  937. if (error)
  938. return -error;
  939. return offset;
  940. }
  941. STATIC loff_t
  942. xfs_file_llseek(
  943. struct file *file,
  944. loff_t offset,
  945. int origin)
  946. {
  947. switch (origin) {
  948. case SEEK_END:
  949. case SEEK_CUR:
  950. case SEEK_SET:
  951. return generic_file_llseek(file, offset, origin);
  952. case SEEK_DATA:
  953. return xfs_seek_data(file, offset, origin);
  954. case SEEK_HOLE:
  955. return xfs_seek_hole(file, offset, origin);
  956. default:
  957. return -EINVAL;
  958. }
  959. }
  960. const struct file_operations xfs_file_operations = {
  961. .llseek = xfs_file_llseek,
  962. .read = do_sync_read,
  963. .write = do_sync_write,
  964. .aio_read = xfs_file_aio_read,
  965. .aio_write = xfs_file_aio_write,
  966. .splice_read = xfs_file_splice_read,
  967. .splice_write = xfs_file_splice_write,
  968. .unlocked_ioctl = xfs_file_ioctl,
  969. #ifdef CONFIG_COMPAT
  970. .compat_ioctl = xfs_file_compat_ioctl,
  971. #endif
  972. .mmap = xfs_file_mmap,
  973. .open = xfs_file_open,
  974. .release = xfs_file_release,
  975. .fsync = xfs_file_fsync,
  976. .fallocate = xfs_file_fallocate,
  977. };
  978. const struct file_operations xfs_dir_file_operations = {
  979. .open = xfs_dir_open,
  980. .read = generic_read_dir,
  981. .readdir = xfs_file_readdir,
  982. .llseek = generic_file_llseek,
  983. .unlocked_ioctl = xfs_file_ioctl,
  984. #ifdef CONFIG_COMPAT
  985. .compat_ioctl = xfs_file_compat_ioctl,
  986. #endif
  987. .fsync = xfs_dir_fsync,
  988. };
  989. static const struct vm_operations_struct xfs_file_vm_ops = {
  990. .fault = filemap_fault,
  991. .page_mkwrite = xfs_vm_page_mkwrite,
  992. };