xfs_file.c 36 KB

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