xfs_file.c 24 KB

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