xfs_file.c 41 KB

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