xfs_file.c 26 KB

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