xfs_file.c 39 KB

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