xfs_file.c 37 KB

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