xfs_file.c 36 KB

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