xfs_file.c 28 KB

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