xfs_file.c 26 KB

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