xfs_file.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_inode_item.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_bmap_util.h"
  34. #include "xfs_error.h"
  35. #include "xfs_dir2.h"
  36. #include "xfs_dir2_priv.h"
  37. #include "xfs_ioctl.h"
  38. #include "xfs_trace.h"
  39. #include "xfs_log.h"
  40. #include "xfs_dinode.h"
  41. #include <linux/aio.h>
  42. #include <linux/dcache.h>
  43. #include <linux/falloc.h>
  44. #include <linux/pagevec.h>
  45. static const struct vm_operations_struct xfs_file_vm_ops;
  46. /*
  47. * Locking primitives for read and write IO paths to ensure we consistently use
  48. * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
  49. */
  50. static inline void
  51. xfs_rw_ilock(
  52. struct xfs_inode *ip,
  53. int type)
  54. {
  55. if (type & XFS_IOLOCK_EXCL)
  56. mutex_lock(&VFS_I(ip)->i_mutex);
  57. xfs_ilock(ip, type);
  58. }
  59. static inline void
  60. xfs_rw_iunlock(
  61. struct xfs_inode *ip,
  62. int type)
  63. {
  64. xfs_iunlock(ip, type);
  65. if (type & XFS_IOLOCK_EXCL)
  66. mutex_unlock(&VFS_I(ip)->i_mutex);
  67. }
  68. static inline void
  69. xfs_rw_ilock_demote(
  70. struct xfs_inode *ip,
  71. int type)
  72. {
  73. xfs_ilock_demote(ip, type);
  74. if (type & XFS_IOLOCK_EXCL)
  75. mutex_unlock(&VFS_I(ip)->i_mutex);
  76. }
  77. /*
  78. * xfs_iozero
  79. *
  80. * xfs_iozero clears the specified range of buffer supplied,
  81. * and marks all the affected blocks as valid and modified. If
  82. * an affected block is not allocated, it will be allocated. If
  83. * an affected block is not completely overwritten, and is not
  84. * valid before the operation, it will be read from disk before
  85. * being partially zeroed.
  86. */
  87. int
  88. xfs_iozero(
  89. struct xfs_inode *ip, /* inode */
  90. loff_t pos, /* offset in file */
  91. size_t count) /* size of data to zero */
  92. {
  93. struct page *page;
  94. struct address_space *mapping;
  95. int status;
  96. mapping = VFS_I(ip)->i_mapping;
  97. do {
  98. unsigned offset, bytes;
  99. void *fsdata;
  100. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  101. bytes = PAGE_CACHE_SIZE - offset;
  102. if (bytes > count)
  103. bytes = count;
  104. status = pagecache_write_begin(NULL, mapping, pos, bytes,
  105. AOP_FLAG_UNINTERRUPTIBLE,
  106. &page, &fsdata);
  107. if (status)
  108. break;
  109. zero_user(page, offset, bytes);
  110. status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
  111. page, fsdata);
  112. WARN_ON(status <= 0); /* can't return less than zero! */
  113. pos += bytes;
  114. count -= bytes;
  115. status = 0;
  116. } while (count);
  117. return (-status);
  118. }
  119. /*
  120. * Fsync operations on directories are much simpler than on regular files,
  121. * as there is no file data to flush, and thus also no need for explicit
  122. * cache flush operations, and there are no non-transaction metadata updates
  123. * on directories either.
  124. */
  125. STATIC int
  126. xfs_dir_fsync(
  127. struct file *file,
  128. loff_t start,
  129. loff_t end,
  130. int datasync)
  131. {
  132. struct xfs_inode *ip = XFS_I(file->f_mapping->host);
  133. struct xfs_mount *mp = ip->i_mount;
  134. xfs_lsn_t lsn = 0;
  135. trace_xfs_dir_fsync(ip);
  136. xfs_ilock(ip, XFS_ILOCK_SHARED);
  137. if (xfs_ipincount(ip))
  138. lsn = ip->i_itemp->ili_last_lsn;
  139. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  140. if (!lsn)
  141. return 0;
  142. return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
  143. }
  144. STATIC int
  145. xfs_file_fsync(
  146. struct file *file,
  147. loff_t start,
  148. loff_t end,
  149. int datasync)
  150. {
  151. struct inode *inode = file->f_mapping->host;
  152. struct xfs_inode *ip = XFS_I(inode);
  153. struct xfs_mount *mp = ip->i_mount;
  154. int error = 0;
  155. int log_flushed = 0;
  156. xfs_lsn_t lsn = 0;
  157. trace_xfs_file_fsync(ip);
  158. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  159. if (error)
  160. return error;
  161. if (XFS_FORCED_SHUTDOWN(mp))
  162. return -XFS_ERROR(EIO);
  163. xfs_iflags_clear(ip, XFS_ITRUNCATED);
  164. if (mp->m_flags & XFS_MOUNT_BARRIER) {
  165. /*
  166. * If we have an RT and/or log subvolume we need to make sure
  167. * to flush the write cache the device used for file data
  168. * first. This is to ensure newly written file data make
  169. * it to disk before logging the new inode size in case of
  170. * an extending write.
  171. */
  172. if (XFS_IS_REALTIME_INODE(ip))
  173. xfs_blkdev_issue_flush(mp->m_rtdev_targp);
  174. else if (mp->m_logdev_targp != mp->m_ddev_targp)
  175. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  176. }
  177. /*
  178. * All metadata updates are logged, which means that we just have
  179. * to flush the log up to the latest LSN that touched the inode.
  180. */
  181. xfs_ilock(ip, XFS_ILOCK_SHARED);
  182. if (xfs_ipincount(ip)) {
  183. if (!datasync ||
  184. (ip->i_itemp->ili_fields & ~XFS_ILOG_TIMESTAMP))
  185. lsn = ip->i_itemp->ili_last_lsn;
  186. }
  187. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  188. if (lsn)
  189. error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
  190. /*
  191. * If we only have a single device, and the log force about was
  192. * a no-op we might have to flush the data device cache here.
  193. * This can only happen for fdatasync/O_DSYNC if we were overwriting
  194. * an already allocated file and thus do not have any metadata to
  195. * commit.
  196. */
  197. if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
  198. mp->m_logdev_targp == mp->m_ddev_targp &&
  199. !XFS_IS_REALTIME_INODE(ip) &&
  200. !log_flushed)
  201. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  202. return -error;
  203. }
  204. STATIC ssize_t
  205. xfs_file_aio_read(
  206. struct kiocb *iocb,
  207. const struct iovec *iovp,
  208. unsigned long nr_segs,
  209. loff_t pos)
  210. {
  211. struct file *file = iocb->ki_filp;
  212. struct inode *inode = file->f_mapping->host;
  213. struct xfs_inode *ip = XFS_I(inode);
  214. struct xfs_mount *mp = ip->i_mount;
  215. size_t size = 0;
  216. ssize_t ret = 0;
  217. int ioflags = 0;
  218. xfs_fsize_t n;
  219. XFS_STATS_INC(xs_read_calls);
  220. BUG_ON(iocb->ki_pos != pos);
  221. if (unlikely(file->f_flags & O_DIRECT))
  222. ioflags |= IO_ISDIRECT;
  223. if (file->f_mode & FMODE_NOCMTIME)
  224. ioflags |= IO_INVIS;
  225. ret = generic_segment_checks(iovp, &nr_segs, &size, VERIFY_WRITE);
  226. if (ret < 0)
  227. return ret;
  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. /* DIO must be aligned to device logical sector size */
  233. if ((pos | size) & target->bt_logical_sectormask) {
  234. if (pos == i_size_read(inode))
  235. return 0;
  236. return -XFS_ERROR(EINVAL);
  237. }
  238. }
  239. n = mp->m_super->s_maxbytes - 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. /*
  247. * Locking is a bit tricky here. If we take an exclusive lock
  248. * for direct IO, we effectively serialise all new concurrent
  249. * read IO to this file and block it behind IO that is currently in
  250. * progress because IO in progress holds the IO lock shared. We only
  251. * need to hold the lock exclusive to blow away the page cache, so
  252. * only take lock exclusively if the page cache needs invalidation.
  253. * This allows the normal direct IO case of no page cache pages to
  254. * proceeed concurrently without serialisation.
  255. */
  256. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  257. if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
  258. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  259. xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
  260. if (inode->i_mapping->nrpages) {
  261. ret = -filemap_write_and_wait_range(
  262. VFS_I(ip)->i_mapping,
  263. pos, -1);
  264. if (ret) {
  265. xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
  266. return ret;
  267. }
  268. truncate_pagecache_range(VFS_I(ip), pos, -1);
  269. }
  270. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  271. }
  272. trace_xfs_file_read(ip, size, pos, ioflags);
  273. ret = generic_file_aio_read(iocb, iovp, nr_segs, pos);
  274. if (ret > 0)
  275. XFS_STATS_ADD(xs_read_bytes, ret);
  276. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  277. return ret;
  278. }
  279. STATIC ssize_t
  280. xfs_file_splice_read(
  281. struct file *infilp,
  282. loff_t *ppos,
  283. struct pipe_inode_info *pipe,
  284. size_t count,
  285. unsigned int flags)
  286. {
  287. struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
  288. int ioflags = 0;
  289. ssize_t ret;
  290. XFS_STATS_INC(xs_read_calls);
  291. if (infilp->f_mode & FMODE_NOCMTIME)
  292. ioflags |= IO_INVIS;
  293. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  294. return -EIO;
  295. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  296. trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
  297. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  298. if (ret > 0)
  299. XFS_STATS_ADD(xs_read_bytes, ret);
  300. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  301. return ret;
  302. }
  303. /*
  304. * xfs_file_splice_write() does not use xfs_rw_ilock() because
  305. * generic_file_splice_write() takes the i_mutex itself. This, in theory,
  306. * couuld cause lock inversions between the aio_write path and the splice path
  307. * if someone is doing concurrent splice(2) based writes and write(2) based
  308. * writes to the same inode. The only real way to fix this is to re-implement
  309. * the generic code here with correct locking orders.
  310. */
  311. STATIC ssize_t
  312. xfs_file_splice_write(
  313. struct pipe_inode_info *pipe,
  314. struct file *outfilp,
  315. loff_t *ppos,
  316. size_t count,
  317. unsigned int flags)
  318. {
  319. struct inode *inode = outfilp->f_mapping->host;
  320. struct xfs_inode *ip = XFS_I(inode);
  321. int ioflags = 0;
  322. ssize_t ret;
  323. XFS_STATS_INC(xs_write_calls);
  324. if (outfilp->f_mode & FMODE_NOCMTIME)
  325. ioflags |= IO_INVIS;
  326. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  327. return -EIO;
  328. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  329. trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
  330. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  331. if (ret > 0)
  332. XFS_STATS_ADD(xs_write_bytes, ret);
  333. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  334. return ret;
  335. }
  336. /*
  337. * This routine is called to handle zeroing any space in the last block of the
  338. * file that is beyond the EOF. We do this since the size is being increased
  339. * without writing anything to that block and we don't want to read the
  340. * garbage on the disk.
  341. */
  342. STATIC int /* error (positive) */
  343. xfs_zero_last_block(
  344. struct xfs_inode *ip,
  345. xfs_fsize_t offset,
  346. xfs_fsize_t isize)
  347. {
  348. struct xfs_mount *mp = ip->i_mount;
  349. xfs_fileoff_t last_fsb = XFS_B_TO_FSBT(mp, isize);
  350. int zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  351. int zero_len;
  352. int nimaps = 1;
  353. int error = 0;
  354. struct xfs_bmbt_irec imap;
  355. xfs_ilock(ip, XFS_ILOCK_EXCL);
  356. error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
  357. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  358. if (error)
  359. return error;
  360. ASSERT(nimaps > 0);
  361. /*
  362. * If the block underlying isize is just a hole, then there
  363. * is nothing to zero.
  364. */
  365. if (imap.br_startblock == HOLESTARTBLOCK)
  366. return 0;
  367. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  368. if (isize + zero_len > offset)
  369. zero_len = offset - isize;
  370. return xfs_iozero(ip, isize, zero_len);
  371. }
  372. /*
  373. * Zero any on disk space between the current EOF and the new, larger EOF.
  374. *
  375. * This handles the normal case of zeroing the remainder of the last block in
  376. * the file and the unusual case of zeroing blocks out beyond the size of the
  377. * file. This second case only happens with fixed size extents and when the
  378. * system crashes before the inode size was updated but after blocks were
  379. * allocated.
  380. *
  381. * Expects the iolock to be held exclusive, and will take the ilock internally.
  382. */
  383. int /* error (positive) */
  384. xfs_zero_eof(
  385. struct xfs_inode *ip,
  386. xfs_off_t offset, /* starting I/O offset */
  387. xfs_fsize_t isize) /* current inode size */
  388. {
  389. struct xfs_mount *mp = ip->i_mount;
  390. xfs_fileoff_t start_zero_fsb;
  391. xfs_fileoff_t end_zero_fsb;
  392. xfs_fileoff_t zero_count_fsb;
  393. xfs_fileoff_t last_fsb;
  394. xfs_fileoff_t zero_off;
  395. xfs_fsize_t zero_len;
  396. int nimaps;
  397. int error = 0;
  398. struct xfs_bmbt_irec imap;
  399. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  400. ASSERT(offset > isize);
  401. /*
  402. * First handle zeroing the block on which isize resides.
  403. *
  404. * We only zero a part of that block so it is handled specially.
  405. */
  406. if (XFS_B_FSB_OFFSET(mp, isize) != 0) {
  407. error = xfs_zero_last_block(ip, offset, isize);
  408. if (error)
  409. return error;
  410. }
  411. /*
  412. * Calculate the range between the new size and the old where blocks
  413. * needing to be zeroed may exist.
  414. *
  415. * To get the block where the last byte in the file currently resides,
  416. * we need to subtract one from the size and truncate back to a block
  417. * boundary. We subtract 1 in case the size is exactly on a block
  418. * boundary.
  419. */
  420. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  421. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  422. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  423. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  424. if (last_fsb == end_zero_fsb) {
  425. /*
  426. * The size was only incremented on its last block.
  427. * We took care of that above, so just return.
  428. */
  429. return 0;
  430. }
  431. ASSERT(start_zero_fsb <= end_zero_fsb);
  432. while (start_zero_fsb <= end_zero_fsb) {
  433. nimaps = 1;
  434. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  435. xfs_ilock(ip, XFS_ILOCK_EXCL);
  436. error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
  437. &imap, &nimaps, 0);
  438. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  439. if (error)
  440. return error;
  441. ASSERT(nimaps > 0);
  442. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  443. imap.br_startblock == HOLESTARTBLOCK) {
  444. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  445. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  446. continue;
  447. }
  448. /*
  449. * There are blocks we need to zero.
  450. */
  451. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  452. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  453. if ((zero_off + zero_len) > offset)
  454. zero_len = offset - zero_off;
  455. error = xfs_iozero(ip, zero_off, zero_len);
  456. if (error)
  457. return error;
  458. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  459. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  460. }
  461. return 0;
  462. }
  463. /*
  464. * Common pre-write limit and setup checks.
  465. *
  466. * Called with the iolocked held either shared and exclusive according to
  467. * @iolock, and returns with it held. Might upgrade the iolock to exclusive
  468. * if called for a direct write beyond i_size.
  469. */
  470. STATIC ssize_t
  471. xfs_file_aio_write_checks(
  472. struct file *file,
  473. loff_t *pos,
  474. size_t *count,
  475. int *iolock)
  476. {
  477. struct inode *inode = file->f_mapping->host;
  478. struct xfs_inode *ip = XFS_I(inode);
  479. int error = 0;
  480. restart:
  481. error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
  482. if (error)
  483. return error;
  484. /*
  485. * If the offset is beyond the size of the file, we need to zero any
  486. * blocks that fall between the existing EOF and the start of this
  487. * write. If zeroing is needed and we are currently holding the
  488. * iolock shared, we need to update it to exclusive which implies
  489. * having to redo all checks before.
  490. */
  491. if (*pos > i_size_read(inode)) {
  492. if (*iolock == XFS_IOLOCK_SHARED) {
  493. xfs_rw_iunlock(ip, *iolock);
  494. *iolock = XFS_IOLOCK_EXCL;
  495. xfs_rw_ilock(ip, *iolock);
  496. goto restart;
  497. }
  498. error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
  499. if (error)
  500. return error;
  501. }
  502. /*
  503. * Updating the timestamps will grab the ilock again from
  504. * xfs_fs_dirty_inode, so we have to call it after dropping the
  505. * lock above. Eventually we should look into a way to avoid
  506. * the pointless lock roundtrip.
  507. */
  508. if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
  509. error = file_update_time(file);
  510. if (error)
  511. return error;
  512. }
  513. /*
  514. * If we're writing the file then make sure to clear the setuid and
  515. * setgid bits if the process is not being run by root. This keeps
  516. * people from modifying setuid and setgid binaries.
  517. */
  518. return file_remove_suid(file);
  519. }
  520. /*
  521. * xfs_file_dio_aio_write - handle direct IO writes
  522. *
  523. * Lock the inode appropriately to prepare for and issue a direct IO write.
  524. * By separating it from the buffered write path we remove all the tricky to
  525. * follow locking changes and looping.
  526. *
  527. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  528. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  529. * pages are flushed out.
  530. *
  531. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  532. * allowing them to be done in parallel with reads and other direct IO writes.
  533. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  534. * needs to do sub-block zeroing and that requires serialisation against other
  535. * direct IOs to the same block. In this case we need to serialise the
  536. * submission of the unaligned IOs so that we don't get racing block zeroing in
  537. * the dio layer. To avoid the problem with aio, we also need to wait for
  538. * outstanding IOs to complete so that unwritten extent conversion is completed
  539. * before we try to map the overlapping block. This is currently implemented by
  540. * hitting it with a big hammer (i.e. inode_dio_wait()).
  541. *
  542. * Returns with locks held indicated by @iolock and errors indicated by
  543. * negative return values.
  544. */
  545. STATIC ssize_t
  546. xfs_file_dio_aio_write(
  547. struct kiocb *iocb,
  548. const struct iovec *iovp,
  549. unsigned long nr_segs,
  550. loff_t pos,
  551. size_t ocount)
  552. {
  553. struct file *file = iocb->ki_filp;
  554. struct address_space *mapping = file->f_mapping;
  555. struct inode *inode = mapping->host;
  556. struct xfs_inode *ip = XFS_I(inode);
  557. struct xfs_mount *mp = ip->i_mount;
  558. ssize_t ret = 0;
  559. size_t count = ocount;
  560. int unaligned_io = 0;
  561. int iolock;
  562. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  563. mp->m_rtdev_targp : mp->m_ddev_targp;
  564. /* DIO must be aligned to device logical sector size */
  565. if ((pos | count) & target->bt_logical_sectormask)
  566. return -XFS_ERROR(EINVAL);
  567. /* "unaligned" here means not aligned to a filesystem block */
  568. if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
  569. unaligned_io = 1;
  570. /*
  571. * We don't need to take an exclusive lock unless there page cache needs
  572. * to be invalidated or unaligned IO is being executed. We don't need to
  573. * consider the EOF extension case here because
  574. * xfs_file_aio_write_checks() will relock the inode as necessary for
  575. * EOF zeroing cases and fill out the new inode size as appropriate.
  576. */
  577. if (unaligned_io || mapping->nrpages)
  578. iolock = XFS_IOLOCK_EXCL;
  579. else
  580. iolock = XFS_IOLOCK_SHARED;
  581. xfs_rw_ilock(ip, iolock);
  582. /*
  583. * Recheck if there are cached pages that need invalidate after we got
  584. * the iolock to protect against other threads adding new pages while
  585. * we were waiting for the iolock.
  586. */
  587. if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
  588. xfs_rw_iunlock(ip, iolock);
  589. iolock = XFS_IOLOCK_EXCL;
  590. xfs_rw_ilock(ip, iolock);
  591. }
  592. ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
  593. if (ret)
  594. goto out;
  595. if (mapping->nrpages) {
  596. ret = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  597. pos, -1);
  598. if (ret)
  599. goto out;
  600. truncate_pagecache_range(VFS_I(ip), pos, -1);
  601. }
  602. /*
  603. * If we are doing unaligned IO, wait for all other IO to drain,
  604. * otherwise demote the lock if we had to flush cached pages
  605. */
  606. if (unaligned_io)
  607. inode_dio_wait(inode);
  608. else if (iolock == XFS_IOLOCK_EXCL) {
  609. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  610. iolock = XFS_IOLOCK_SHARED;
  611. }
  612. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
  613. ret = generic_file_direct_write(iocb, iovp,
  614. &nr_segs, pos, &iocb->ki_pos, count, ocount);
  615. out:
  616. xfs_rw_iunlock(ip, iolock);
  617. /* No fallback to buffered IO on errors for XFS. */
  618. ASSERT(ret < 0 || ret == count);
  619. return ret;
  620. }
  621. STATIC ssize_t
  622. xfs_file_buffered_aio_write(
  623. struct kiocb *iocb,
  624. const struct iovec *iovp,
  625. unsigned long nr_segs,
  626. loff_t pos,
  627. size_t ocount)
  628. {
  629. struct file *file = iocb->ki_filp;
  630. struct address_space *mapping = file->f_mapping;
  631. struct inode *inode = mapping->host;
  632. struct xfs_inode *ip = XFS_I(inode);
  633. ssize_t ret;
  634. int enospc = 0;
  635. int iolock = XFS_IOLOCK_EXCL;
  636. size_t count = ocount;
  637. xfs_rw_ilock(ip, iolock);
  638. ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
  639. if (ret)
  640. goto out;
  641. /* We can write back this queue in page reclaim */
  642. current->backing_dev_info = mapping->backing_dev_info;
  643. write_retry:
  644. trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
  645. ret = generic_file_buffered_write(iocb, iovp, nr_segs,
  646. pos, &iocb->ki_pos, count, 0);
  647. /*
  648. * If we just got an ENOSPC, try to write back all dirty inodes to
  649. * convert delalloc space to free up some of the excess reserved
  650. * metadata space.
  651. */
  652. if (ret == -ENOSPC && !enospc) {
  653. enospc = 1;
  654. xfs_flush_inodes(ip->i_mount);
  655. goto write_retry;
  656. }
  657. current->backing_dev_info = NULL;
  658. out:
  659. xfs_rw_iunlock(ip, iolock);
  660. return ret;
  661. }
  662. STATIC ssize_t
  663. xfs_file_aio_write(
  664. struct kiocb *iocb,
  665. const struct iovec *iovp,
  666. unsigned long nr_segs,
  667. loff_t pos)
  668. {
  669. struct file *file = iocb->ki_filp;
  670. struct address_space *mapping = file->f_mapping;
  671. struct inode *inode = mapping->host;
  672. struct xfs_inode *ip = XFS_I(inode);
  673. ssize_t ret;
  674. size_t ocount = 0;
  675. XFS_STATS_INC(xs_write_calls);
  676. BUG_ON(iocb->ki_pos != pos);
  677. ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
  678. if (ret)
  679. return ret;
  680. if (ocount == 0)
  681. return 0;
  682. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  683. ret = -EIO;
  684. goto out;
  685. }
  686. if (unlikely(file->f_flags & O_DIRECT))
  687. ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, ocount);
  688. else
  689. ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
  690. ocount);
  691. if (ret > 0) {
  692. ssize_t err;
  693. XFS_STATS_ADD(xs_write_bytes, ret);
  694. /* Handle various SYNC-type writes */
  695. err = generic_write_sync(file, pos, ret);
  696. if (err < 0)
  697. ret = err;
  698. }
  699. out:
  700. return ret;
  701. }
  702. STATIC long
  703. xfs_file_fallocate(
  704. struct file *file,
  705. int mode,
  706. loff_t offset,
  707. loff_t len)
  708. {
  709. struct inode *inode = file_inode(file);
  710. struct xfs_inode *ip = XFS_I(inode);
  711. struct xfs_trans *tp;
  712. long error;
  713. loff_t new_size = 0;
  714. if (!S_ISREG(inode->i_mode))
  715. return -EINVAL;
  716. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  717. return -EOPNOTSUPP;
  718. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  719. if (mode & FALLOC_FL_PUNCH_HOLE) {
  720. error = xfs_free_file_space(ip, offset, len);
  721. if (error)
  722. goto out_unlock;
  723. } else {
  724. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  725. offset + len > i_size_read(inode)) {
  726. new_size = offset + len;
  727. error = -inode_newsize_ok(inode, new_size);
  728. if (error)
  729. goto out_unlock;
  730. }
  731. error = xfs_alloc_file_space(ip, offset, len,
  732. XFS_BMAPI_PREALLOC);
  733. if (error)
  734. goto out_unlock;
  735. }
  736. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_WRITEID);
  737. error = xfs_trans_reserve(tp, &M_RES(ip->i_mount)->tr_writeid, 0, 0);
  738. if (error) {
  739. xfs_trans_cancel(tp, 0);
  740. goto out_unlock;
  741. }
  742. xfs_ilock(ip, XFS_ILOCK_EXCL);
  743. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  744. ip->i_d.di_mode &= ~S_ISUID;
  745. if (ip->i_d.di_mode & S_IXGRP)
  746. ip->i_d.di_mode &= ~S_ISGID;
  747. if (!(mode & FALLOC_FL_PUNCH_HOLE))
  748. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  749. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  750. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  751. if (file->f_flags & O_DSYNC)
  752. xfs_trans_set_sync(tp);
  753. error = xfs_trans_commit(tp, 0);
  754. if (error)
  755. goto out_unlock;
  756. /* Change file size if needed */
  757. if (new_size) {
  758. struct iattr iattr;
  759. iattr.ia_valid = ATTR_SIZE;
  760. iattr.ia_size = new_size;
  761. error = xfs_setattr_size(ip, &iattr);
  762. }
  763. out_unlock:
  764. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  765. return -error;
  766. }
  767. STATIC int
  768. xfs_file_open(
  769. struct inode *inode,
  770. struct file *file)
  771. {
  772. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  773. return -EFBIG;
  774. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  775. return -EIO;
  776. return 0;
  777. }
  778. STATIC int
  779. xfs_dir_open(
  780. struct inode *inode,
  781. struct file *file)
  782. {
  783. struct xfs_inode *ip = XFS_I(inode);
  784. int mode;
  785. int error;
  786. error = xfs_file_open(inode, file);
  787. if (error)
  788. return error;
  789. /*
  790. * If there are any blocks, read-ahead block 0 as we're almost
  791. * certain to have the next operation be a read there.
  792. */
  793. mode = xfs_ilock_data_map_shared(ip);
  794. if (ip->i_d.di_nextents > 0)
  795. xfs_dir3_data_readahead(NULL, ip, 0, -1);
  796. xfs_iunlock(ip, mode);
  797. return 0;
  798. }
  799. STATIC int
  800. xfs_file_release(
  801. struct inode *inode,
  802. struct file *filp)
  803. {
  804. return -xfs_release(XFS_I(inode));
  805. }
  806. STATIC int
  807. xfs_file_readdir(
  808. struct file *file,
  809. struct dir_context *ctx)
  810. {
  811. struct inode *inode = file_inode(file);
  812. xfs_inode_t *ip = XFS_I(inode);
  813. int error;
  814. size_t bufsize;
  815. /*
  816. * The Linux API doesn't pass down the total size of the buffer
  817. * we read into down to the filesystem. With the filldir concept
  818. * it's not needed for correct information, but the XFS dir2 leaf
  819. * code wants an estimate of the buffer size to calculate it's
  820. * readahead window and size the buffers used for mapping to
  821. * physical blocks.
  822. *
  823. * Try to give it an estimate that's good enough, maybe at some
  824. * point we can change the ->readdir prototype to include the
  825. * buffer size. For now we use the current glibc buffer size.
  826. */
  827. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  828. error = xfs_readdir(ip, ctx, bufsize);
  829. if (error)
  830. return -error;
  831. return 0;
  832. }
  833. STATIC int
  834. xfs_file_mmap(
  835. struct file *filp,
  836. struct vm_area_struct *vma)
  837. {
  838. vma->vm_ops = &xfs_file_vm_ops;
  839. file_accessed(filp);
  840. return 0;
  841. }
  842. /*
  843. * mmap()d file has taken write protection fault and is being made
  844. * writable. We can set the page state up correctly for a writable
  845. * page, which means we can do correct delalloc accounting (ENOSPC
  846. * checking!) and unwritten extent mapping.
  847. */
  848. STATIC int
  849. xfs_vm_page_mkwrite(
  850. struct vm_area_struct *vma,
  851. struct vm_fault *vmf)
  852. {
  853. return block_page_mkwrite(vma, vmf, xfs_get_blocks);
  854. }
  855. /*
  856. * This type is designed to indicate the type of offset we would like
  857. * to search from page cache for either xfs_seek_data() or xfs_seek_hole().
  858. */
  859. enum {
  860. HOLE_OFF = 0,
  861. DATA_OFF,
  862. };
  863. /*
  864. * Lookup the desired type of offset from the given page.
  865. *
  866. * On success, return true and the offset argument will point to the
  867. * start of the region that was found. Otherwise this function will
  868. * return false and keep the offset argument unchanged.
  869. */
  870. STATIC bool
  871. xfs_lookup_buffer_offset(
  872. struct page *page,
  873. loff_t *offset,
  874. unsigned int type)
  875. {
  876. loff_t lastoff = page_offset(page);
  877. bool found = false;
  878. struct buffer_head *bh, *head;
  879. bh = head = page_buffers(page);
  880. do {
  881. /*
  882. * Unwritten extents that have data in the page
  883. * cache covering them can be identified by the
  884. * BH_Unwritten state flag. Pages with multiple
  885. * buffers might have a mix of holes, data and
  886. * unwritten extents - any buffer with valid
  887. * data in it should have BH_Uptodate flag set
  888. * on it.
  889. */
  890. if (buffer_unwritten(bh) ||
  891. buffer_uptodate(bh)) {
  892. if (type == DATA_OFF)
  893. found = true;
  894. } else {
  895. if (type == HOLE_OFF)
  896. found = true;
  897. }
  898. if (found) {
  899. *offset = lastoff;
  900. break;
  901. }
  902. lastoff += bh->b_size;
  903. } while ((bh = bh->b_this_page) != head);
  904. return found;
  905. }
  906. /*
  907. * This routine is called to find out and return a data or hole offset
  908. * from the page cache for unwritten extents according to the desired
  909. * type for xfs_seek_data() or xfs_seek_hole().
  910. *
  911. * The argument offset is used to tell where we start to search from the
  912. * page cache. Map is used to figure out the end points of the range to
  913. * lookup pages.
  914. *
  915. * Return true if the desired type of offset was found, and the argument
  916. * offset is filled with that address. Otherwise, return false and keep
  917. * offset unchanged.
  918. */
  919. STATIC bool
  920. xfs_find_get_desired_pgoff(
  921. struct inode *inode,
  922. struct xfs_bmbt_irec *map,
  923. unsigned int type,
  924. loff_t *offset)
  925. {
  926. struct xfs_inode *ip = XFS_I(inode);
  927. struct xfs_mount *mp = ip->i_mount;
  928. struct pagevec pvec;
  929. pgoff_t index;
  930. pgoff_t end;
  931. loff_t endoff;
  932. loff_t startoff = *offset;
  933. loff_t lastoff = startoff;
  934. bool found = false;
  935. pagevec_init(&pvec, 0);
  936. index = startoff >> PAGE_CACHE_SHIFT;
  937. endoff = XFS_FSB_TO_B(mp, map->br_startoff + map->br_blockcount);
  938. end = endoff >> PAGE_CACHE_SHIFT;
  939. do {
  940. int want;
  941. unsigned nr_pages;
  942. unsigned int i;
  943. want = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
  944. nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
  945. want);
  946. /*
  947. * No page mapped into given range. If we are searching holes
  948. * and if this is the first time we got into the loop, it means
  949. * that the given offset is landed in a hole, return it.
  950. *
  951. * If we have already stepped through some block buffers to find
  952. * holes but they all contains data. In this case, the last
  953. * offset is already updated and pointed to the end of the last
  954. * mapped page, if it does not reach the endpoint to search,
  955. * that means there should be a hole between them.
  956. */
  957. if (nr_pages == 0) {
  958. /* Data search found nothing */
  959. if (type == DATA_OFF)
  960. break;
  961. ASSERT(type == HOLE_OFF);
  962. if (lastoff == startoff || lastoff < endoff) {
  963. found = true;
  964. *offset = lastoff;
  965. }
  966. break;
  967. }
  968. /*
  969. * At lease we found one page. If this is the first time we
  970. * step into the loop, and if the first page index offset is
  971. * greater than the given search offset, a hole was found.
  972. */
  973. if (type == HOLE_OFF && lastoff == startoff &&
  974. lastoff < page_offset(pvec.pages[0])) {
  975. found = true;
  976. break;
  977. }
  978. for (i = 0; i < nr_pages; i++) {
  979. struct page *page = pvec.pages[i];
  980. loff_t b_offset;
  981. /*
  982. * At this point, the page may be truncated or
  983. * invalidated (changing page->mapping to NULL),
  984. * or even swizzled back from swapper_space to tmpfs
  985. * file mapping. However, page->index will not change
  986. * because we have a reference on the page.
  987. *
  988. * Searching done if the page index is out of range.
  989. * If the current offset is not reaches the end of
  990. * the specified search range, there should be a hole
  991. * between them.
  992. */
  993. if (page->index > end) {
  994. if (type == HOLE_OFF && lastoff < endoff) {
  995. *offset = lastoff;
  996. found = true;
  997. }
  998. goto out;
  999. }
  1000. lock_page(page);
  1001. /*
  1002. * Page truncated or invalidated(page->mapping == NULL).
  1003. * We can freely skip it and proceed to check the next
  1004. * page.
  1005. */
  1006. if (unlikely(page->mapping != inode->i_mapping)) {
  1007. unlock_page(page);
  1008. continue;
  1009. }
  1010. if (!page_has_buffers(page)) {
  1011. unlock_page(page);
  1012. continue;
  1013. }
  1014. found = xfs_lookup_buffer_offset(page, &b_offset, type);
  1015. if (found) {
  1016. /*
  1017. * The found offset may be less than the start
  1018. * point to search if this is the first time to
  1019. * come here.
  1020. */
  1021. *offset = max_t(loff_t, startoff, b_offset);
  1022. unlock_page(page);
  1023. goto out;
  1024. }
  1025. /*
  1026. * We either searching data but nothing was found, or
  1027. * searching hole but found a data buffer. In either
  1028. * case, probably the next page contains the desired
  1029. * things, update the last offset to it so.
  1030. */
  1031. lastoff = page_offset(page) + PAGE_SIZE;
  1032. unlock_page(page);
  1033. }
  1034. /*
  1035. * The number of returned pages less than our desired, search
  1036. * done. In this case, nothing was found for searching data,
  1037. * but we found a hole behind the last offset.
  1038. */
  1039. if (nr_pages < want) {
  1040. if (type == HOLE_OFF) {
  1041. *offset = lastoff;
  1042. found = true;
  1043. }
  1044. break;
  1045. }
  1046. index = pvec.pages[i - 1]->index + 1;
  1047. pagevec_release(&pvec);
  1048. } while (index <= end);
  1049. out:
  1050. pagevec_release(&pvec);
  1051. return found;
  1052. }
  1053. STATIC loff_t
  1054. xfs_seek_data(
  1055. struct file *file,
  1056. loff_t start)
  1057. {
  1058. struct inode *inode = file->f_mapping->host;
  1059. struct xfs_inode *ip = XFS_I(inode);
  1060. struct xfs_mount *mp = ip->i_mount;
  1061. loff_t uninitialized_var(offset);
  1062. xfs_fsize_t isize;
  1063. xfs_fileoff_t fsbno;
  1064. xfs_filblks_t end;
  1065. uint lock;
  1066. int error;
  1067. lock = xfs_ilock_data_map_shared(ip);
  1068. isize = i_size_read(inode);
  1069. if (start >= isize) {
  1070. error = ENXIO;
  1071. goto out_unlock;
  1072. }
  1073. /*
  1074. * Try to read extents from the first block indicated
  1075. * by fsbno to the end block of the file.
  1076. */
  1077. fsbno = XFS_B_TO_FSBT(mp, start);
  1078. end = XFS_B_TO_FSB(mp, isize);
  1079. for (;;) {
  1080. struct xfs_bmbt_irec map[2];
  1081. int nmap = 2;
  1082. unsigned int i;
  1083. error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
  1084. XFS_BMAPI_ENTIRE);
  1085. if (error)
  1086. goto out_unlock;
  1087. /* No extents at given offset, must be beyond EOF */
  1088. if (nmap == 0) {
  1089. error = ENXIO;
  1090. goto out_unlock;
  1091. }
  1092. for (i = 0; i < nmap; i++) {
  1093. offset = max_t(loff_t, start,
  1094. XFS_FSB_TO_B(mp, map[i].br_startoff));
  1095. /* Landed in a data extent */
  1096. if (map[i].br_startblock == DELAYSTARTBLOCK ||
  1097. (map[i].br_state == XFS_EXT_NORM &&
  1098. !isnullstartblock(map[i].br_startblock)))
  1099. goto out;
  1100. /*
  1101. * Landed in an unwritten extent, try to search data
  1102. * from page cache.
  1103. */
  1104. if (map[i].br_state == XFS_EXT_UNWRITTEN) {
  1105. if (xfs_find_get_desired_pgoff(inode, &map[i],
  1106. DATA_OFF, &offset))
  1107. goto out;
  1108. }
  1109. }
  1110. /*
  1111. * map[0] is hole or its an unwritten extent but
  1112. * without data in page cache. Probably means that
  1113. * we are reading after EOF if nothing in map[1].
  1114. */
  1115. if (nmap == 1) {
  1116. error = ENXIO;
  1117. goto out_unlock;
  1118. }
  1119. ASSERT(i > 1);
  1120. /*
  1121. * Nothing was found, proceed to the next round of search
  1122. * if reading offset not beyond or hit EOF.
  1123. */
  1124. fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
  1125. start = XFS_FSB_TO_B(mp, fsbno);
  1126. if (start >= isize) {
  1127. error = ENXIO;
  1128. goto out_unlock;
  1129. }
  1130. }
  1131. out:
  1132. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  1133. out_unlock:
  1134. xfs_iunlock(ip, lock);
  1135. if (error)
  1136. return -error;
  1137. return offset;
  1138. }
  1139. STATIC loff_t
  1140. xfs_seek_hole(
  1141. struct file *file,
  1142. loff_t start)
  1143. {
  1144. struct inode *inode = file->f_mapping->host;
  1145. struct xfs_inode *ip = XFS_I(inode);
  1146. struct xfs_mount *mp = ip->i_mount;
  1147. loff_t uninitialized_var(offset);
  1148. xfs_fsize_t isize;
  1149. xfs_fileoff_t fsbno;
  1150. xfs_filblks_t end;
  1151. uint lock;
  1152. int error;
  1153. if (XFS_FORCED_SHUTDOWN(mp))
  1154. return -XFS_ERROR(EIO);
  1155. lock = xfs_ilock_data_map_shared(ip);
  1156. isize = i_size_read(inode);
  1157. if (start >= isize) {
  1158. error = ENXIO;
  1159. goto out_unlock;
  1160. }
  1161. fsbno = XFS_B_TO_FSBT(mp, start);
  1162. end = XFS_B_TO_FSB(mp, isize);
  1163. for (;;) {
  1164. struct xfs_bmbt_irec map[2];
  1165. int nmap = 2;
  1166. unsigned int i;
  1167. error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
  1168. XFS_BMAPI_ENTIRE);
  1169. if (error)
  1170. goto out_unlock;
  1171. /* No extents at given offset, must be beyond EOF */
  1172. if (nmap == 0) {
  1173. error = ENXIO;
  1174. goto out_unlock;
  1175. }
  1176. for (i = 0; i < nmap; i++) {
  1177. offset = max_t(loff_t, start,
  1178. XFS_FSB_TO_B(mp, map[i].br_startoff));
  1179. /* Landed in a hole */
  1180. if (map[i].br_startblock == HOLESTARTBLOCK)
  1181. goto out;
  1182. /*
  1183. * Landed in an unwritten extent, try to search hole
  1184. * from page cache.
  1185. */
  1186. if (map[i].br_state == XFS_EXT_UNWRITTEN) {
  1187. if (xfs_find_get_desired_pgoff(inode, &map[i],
  1188. HOLE_OFF, &offset))
  1189. goto out;
  1190. }
  1191. }
  1192. /*
  1193. * map[0] contains data or its unwritten but contains
  1194. * data in page cache, probably means that we are
  1195. * reading after EOF. We should fix offset to point
  1196. * to the end of the file(i.e., there is an implicit
  1197. * hole at the end of any file).
  1198. */
  1199. if (nmap == 1) {
  1200. offset = isize;
  1201. break;
  1202. }
  1203. ASSERT(i > 1);
  1204. /*
  1205. * Both mappings contains data, proceed to the next round of
  1206. * search if the current reading offset not beyond or hit EOF.
  1207. */
  1208. fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
  1209. start = XFS_FSB_TO_B(mp, fsbno);
  1210. if (start >= isize) {
  1211. offset = isize;
  1212. break;
  1213. }
  1214. }
  1215. out:
  1216. /*
  1217. * At this point, we must have found a hole. However, the returned
  1218. * offset may be bigger than the file size as it may be aligned to
  1219. * page boundary for unwritten extents, we need to deal with this
  1220. * situation in particular.
  1221. */
  1222. offset = min_t(loff_t, offset, isize);
  1223. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  1224. out_unlock:
  1225. xfs_iunlock(ip, lock);
  1226. if (error)
  1227. return -error;
  1228. return offset;
  1229. }
  1230. STATIC loff_t
  1231. xfs_file_llseek(
  1232. struct file *file,
  1233. loff_t offset,
  1234. int origin)
  1235. {
  1236. switch (origin) {
  1237. case SEEK_END:
  1238. case SEEK_CUR:
  1239. case SEEK_SET:
  1240. return generic_file_llseek(file, offset, origin);
  1241. case SEEK_DATA:
  1242. return xfs_seek_data(file, offset);
  1243. case SEEK_HOLE:
  1244. return xfs_seek_hole(file, offset);
  1245. default:
  1246. return -EINVAL;
  1247. }
  1248. }
  1249. const struct file_operations xfs_file_operations = {
  1250. .llseek = xfs_file_llseek,
  1251. .read = do_sync_read,
  1252. .write = do_sync_write,
  1253. .aio_read = xfs_file_aio_read,
  1254. .aio_write = xfs_file_aio_write,
  1255. .splice_read = xfs_file_splice_read,
  1256. .splice_write = xfs_file_splice_write,
  1257. .unlocked_ioctl = xfs_file_ioctl,
  1258. #ifdef CONFIG_COMPAT
  1259. .compat_ioctl = xfs_file_compat_ioctl,
  1260. #endif
  1261. .mmap = xfs_file_mmap,
  1262. .open = xfs_file_open,
  1263. .release = xfs_file_release,
  1264. .fsync = xfs_file_fsync,
  1265. .fallocate = xfs_file_fallocate,
  1266. };
  1267. const struct file_operations xfs_dir_file_operations = {
  1268. .open = xfs_dir_open,
  1269. .read = generic_read_dir,
  1270. .iterate = xfs_file_readdir,
  1271. .llseek = generic_file_llseek,
  1272. .unlocked_ioctl = xfs_file_ioctl,
  1273. #ifdef CONFIG_COMPAT
  1274. .compat_ioctl = xfs_file_compat_ioctl,
  1275. #endif
  1276. .fsync = xfs_dir_fsync,
  1277. };
  1278. static const struct vm_operations_struct xfs_file_vm_ops = {
  1279. .fault = filemap_fault,
  1280. .page_mkwrite = xfs_vm_page_mkwrite,
  1281. .remap_pages = generic_file_remap_pages,
  1282. };