xfs_file.c 23 KB

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