xfs_file.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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. /* For changing security info in file_remove_privs() we need i_mutex */
  492. if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
  493. xfs_rw_iunlock(ip, *iolock);
  494. *iolock = XFS_IOLOCK_EXCL;
  495. xfs_rw_ilock(ip, *iolock);
  496. goto restart;
  497. }
  498. /*
  499. * If the offset is beyond the size of the file, we need to zero any
  500. * blocks that fall between the existing EOF and the start of this
  501. * write. If zeroing is needed and we are currently holding the
  502. * iolock shared, we need to update it to exclusive which implies
  503. * having to redo all checks before.
  504. *
  505. * We need to serialise against EOF updates that occur in IO
  506. * completions here. We want to make sure that nobody is changing the
  507. * size while we do this check until we have placed an IO barrier (i.e.
  508. * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
  509. * The spinlock effectively forms a memory barrier once we have the
  510. * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
  511. * and hence be able to correctly determine if we need to run zeroing.
  512. */
  513. spin_lock(&ip->i_flags_lock);
  514. if (iocb->ki_pos > i_size_read(inode)) {
  515. bool zero = false;
  516. spin_unlock(&ip->i_flags_lock);
  517. if (*iolock == XFS_IOLOCK_SHARED) {
  518. xfs_rw_iunlock(ip, *iolock);
  519. *iolock = XFS_IOLOCK_EXCL;
  520. xfs_rw_ilock(ip, *iolock);
  521. iov_iter_reexpand(from, count);
  522. /*
  523. * We now have an IO submission barrier in place, but
  524. * AIO can do EOF updates during IO completion and hence
  525. * we now need to wait for all of them to drain. Non-AIO
  526. * DIO will have drained before we are given the
  527. * XFS_IOLOCK_EXCL, and so for most cases this wait is a
  528. * no-op.
  529. */
  530. inode_dio_wait(inode);
  531. goto restart;
  532. }
  533. error = xfs_zero_eof(ip, iocb->ki_pos, i_size_read(inode), &zero);
  534. if (error)
  535. return error;
  536. } else
  537. spin_unlock(&ip->i_flags_lock);
  538. /*
  539. * Updating the timestamps will grab the ilock again from
  540. * xfs_fs_dirty_inode, so we have to call it after dropping the
  541. * lock above. Eventually we should look into a way to avoid
  542. * the pointless lock roundtrip.
  543. */
  544. if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
  545. error = file_update_time(file);
  546. if (error)
  547. return error;
  548. }
  549. /*
  550. * If we're writing the file then make sure to clear the setuid and
  551. * setgid bits if the process is not being run by root. This keeps
  552. * people from modifying setuid and setgid binaries.
  553. */
  554. if (!IS_NOSEC(inode))
  555. return file_remove_privs(file);
  556. return 0;
  557. }
  558. /*
  559. * xfs_file_dio_aio_write - handle direct IO writes
  560. *
  561. * Lock the inode appropriately to prepare for and issue a direct IO write.
  562. * By separating it from the buffered write path we remove all the tricky to
  563. * follow locking changes and looping.
  564. *
  565. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  566. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  567. * pages are flushed out.
  568. *
  569. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  570. * allowing them to be done in parallel with reads and other direct IO writes.
  571. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  572. * needs to do sub-block zeroing and that requires serialisation against other
  573. * direct IOs to the same block. In this case we need to serialise the
  574. * submission of the unaligned IOs so that we don't get racing block zeroing in
  575. * the dio layer. To avoid the problem with aio, we also need to wait for
  576. * outstanding IOs to complete so that unwritten extent conversion is completed
  577. * before we try to map the overlapping block. This is currently implemented by
  578. * hitting it with a big hammer (i.e. inode_dio_wait()).
  579. *
  580. * Returns with locks held indicated by @iolock and errors indicated by
  581. * negative return values.
  582. */
  583. STATIC ssize_t
  584. xfs_file_dio_aio_write(
  585. struct kiocb *iocb,
  586. struct iov_iter *from)
  587. {
  588. struct file *file = iocb->ki_filp;
  589. struct address_space *mapping = file->f_mapping;
  590. struct inode *inode = mapping->host;
  591. struct xfs_inode *ip = XFS_I(inode);
  592. struct xfs_mount *mp = ip->i_mount;
  593. ssize_t ret = 0;
  594. int unaligned_io = 0;
  595. int iolock;
  596. size_t count = iov_iter_count(from);
  597. loff_t pos = iocb->ki_pos;
  598. loff_t end;
  599. struct iov_iter data;
  600. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  601. mp->m_rtdev_targp : mp->m_ddev_targp;
  602. /* DIO must be aligned to device logical sector size */
  603. if ((pos | count) & target->bt_logical_sectormask)
  604. return -EINVAL;
  605. /* "unaligned" here means not aligned to a filesystem block */
  606. if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
  607. unaligned_io = 1;
  608. /*
  609. * We don't need to take an exclusive lock unless there page cache needs
  610. * to be invalidated or unaligned IO is being executed. We don't need to
  611. * consider the EOF extension case here because
  612. * xfs_file_aio_write_checks() will relock the inode as necessary for
  613. * EOF zeroing cases and fill out the new inode size as appropriate.
  614. */
  615. if (unaligned_io || mapping->nrpages)
  616. iolock = XFS_IOLOCK_EXCL;
  617. else
  618. iolock = XFS_IOLOCK_SHARED;
  619. xfs_rw_ilock(ip, iolock);
  620. /*
  621. * Recheck if there are cached pages that need invalidate after we got
  622. * the iolock to protect against other threads adding new pages while
  623. * we were waiting for the iolock.
  624. */
  625. if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
  626. xfs_rw_iunlock(ip, iolock);
  627. iolock = XFS_IOLOCK_EXCL;
  628. xfs_rw_ilock(ip, iolock);
  629. }
  630. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  631. if (ret)
  632. goto out;
  633. count = iov_iter_count(from);
  634. pos = iocb->ki_pos;
  635. end = pos + count - 1;
  636. if (mapping->nrpages) {
  637. ret = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  638. pos, end);
  639. if (ret)
  640. goto out;
  641. /*
  642. * Invalidate whole pages. This can return an error if
  643. * we fail to invalidate a page, but this should never
  644. * happen on XFS. Warn if it does fail.
  645. */
  646. ret = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
  647. pos >> PAGE_CACHE_SHIFT,
  648. end >> PAGE_CACHE_SHIFT);
  649. WARN_ON_ONCE(ret);
  650. ret = 0;
  651. }
  652. /*
  653. * If we are doing unaligned IO, wait for all other IO to drain,
  654. * otherwise demote the lock if we had to flush cached pages
  655. */
  656. if (unaligned_io)
  657. inode_dio_wait(inode);
  658. else if (iolock == XFS_IOLOCK_EXCL) {
  659. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  660. iolock = XFS_IOLOCK_SHARED;
  661. }
  662. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
  663. data = *from;
  664. ret = mapping->a_ops->direct_IO(iocb, &data, pos);
  665. /* see generic_file_direct_write() for why this is necessary */
  666. if (mapping->nrpages) {
  667. invalidate_inode_pages2_range(mapping,
  668. pos >> PAGE_CACHE_SHIFT,
  669. end >> PAGE_CACHE_SHIFT);
  670. }
  671. if (ret > 0) {
  672. pos += ret;
  673. iov_iter_advance(from, ret);
  674. iocb->ki_pos = pos;
  675. }
  676. out:
  677. xfs_rw_iunlock(ip, iolock);
  678. /* No fallback to buffered IO on errors for XFS. */
  679. ASSERT(ret < 0 || ret == count);
  680. return ret;
  681. }
  682. STATIC ssize_t
  683. xfs_file_buffered_aio_write(
  684. struct kiocb *iocb,
  685. struct iov_iter *from)
  686. {
  687. struct file *file = iocb->ki_filp;
  688. struct address_space *mapping = file->f_mapping;
  689. struct inode *inode = mapping->host;
  690. struct xfs_inode *ip = XFS_I(inode);
  691. ssize_t ret;
  692. int enospc = 0;
  693. int iolock = XFS_IOLOCK_EXCL;
  694. xfs_rw_ilock(ip, iolock);
  695. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  696. if (ret)
  697. goto out;
  698. /* We can write back this queue in page reclaim */
  699. current->backing_dev_info = inode_to_bdi(inode);
  700. write_retry:
  701. trace_xfs_file_buffered_write(ip, iov_iter_count(from),
  702. iocb->ki_pos, 0);
  703. ret = generic_perform_write(file, from, iocb->ki_pos);
  704. if (likely(ret >= 0))
  705. iocb->ki_pos += ret;
  706. /*
  707. * If we hit a space limit, try to free up some lingering preallocated
  708. * space before returning an error. In the case of ENOSPC, first try to
  709. * write back all dirty inodes to free up some of the excess reserved
  710. * metadata space. This reduces the chances that the eofblocks scan
  711. * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
  712. * also behaves as a filter to prevent too many eofblocks scans from
  713. * running at the same time.
  714. */
  715. if (ret == -EDQUOT && !enospc) {
  716. enospc = xfs_inode_free_quota_eofblocks(ip);
  717. if (enospc)
  718. goto write_retry;
  719. } else if (ret == -ENOSPC && !enospc) {
  720. struct xfs_eofblocks eofb = {0};
  721. enospc = 1;
  722. xfs_flush_inodes(ip->i_mount);
  723. eofb.eof_scan_owner = ip->i_ino; /* for locking */
  724. eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
  725. xfs_icache_free_eofblocks(ip->i_mount, &eofb);
  726. goto write_retry;
  727. }
  728. current->backing_dev_info = NULL;
  729. out:
  730. xfs_rw_iunlock(ip, iolock);
  731. return ret;
  732. }
  733. STATIC ssize_t
  734. xfs_file_write_iter(
  735. struct kiocb *iocb,
  736. struct iov_iter *from)
  737. {
  738. struct file *file = iocb->ki_filp;
  739. struct address_space *mapping = file->f_mapping;
  740. struct inode *inode = mapping->host;
  741. struct xfs_inode *ip = XFS_I(inode);
  742. ssize_t ret;
  743. size_t ocount = iov_iter_count(from);
  744. XFS_STATS_INC(xs_write_calls);
  745. if (ocount == 0)
  746. return 0;
  747. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  748. return -EIO;
  749. if (unlikely(iocb->ki_flags & IOCB_DIRECT))
  750. ret = xfs_file_dio_aio_write(iocb, from);
  751. else
  752. ret = xfs_file_buffered_aio_write(iocb, from);
  753. if (ret > 0) {
  754. ssize_t err;
  755. XFS_STATS_ADD(xs_write_bytes, ret);
  756. /* Handle various SYNC-type writes */
  757. err = generic_write_sync(file, iocb->ki_pos - ret, ret);
  758. if (err < 0)
  759. ret = err;
  760. }
  761. return ret;
  762. }
  763. #define XFS_FALLOC_FL_SUPPORTED \
  764. (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
  765. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
  766. FALLOC_FL_INSERT_RANGE)
  767. STATIC long
  768. xfs_file_fallocate(
  769. struct file *file,
  770. int mode,
  771. loff_t offset,
  772. loff_t len)
  773. {
  774. struct inode *inode = file_inode(file);
  775. struct xfs_inode *ip = XFS_I(inode);
  776. long error;
  777. enum xfs_prealloc_flags flags = 0;
  778. uint iolock = XFS_IOLOCK_EXCL;
  779. loff_t new_size = 0;
  780. bool do_file_insert = 0;
  781. if (!S_ISREG(inode->i_mode))
  782. return -EINVAL;
  783. if (mode & ~XFS_FALLOC_FL_SUPPORTED)
  784. return -EOPNOTSUPP;
  785. xfs_ilock(ip, iolock);
  786. error = xfs_break_layouts(inode, &iolock, false);
  787. if (error)
  788. goto out_unlock;
  789. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  790. iolock |= XFS_MMAPLOCK_EXCL;
  791. if (mode & FALLOC_FL_PUNCH_HOLE) {
  792. error = xfs_free_file_space(ip, offset, len);
  793. if (error)
  794. goto out_unlock;
  795. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  796. unsigned blksize_mask = (1 << inode->i_blkbits) - 1;
  797. if (offset & blksize_mask || len & blksize_mask) {
  798. error = -EINVAL;
  799. goto out_unlock;
  800. }
  801. /*
  802. * There is no need to overlap collapse range with EOF,
  803. * in which case it is effectively a truncate operation
  804. */
  805. if (offset + len >= i_size_read(inode)) {
  806. error = -EINVAL;
  807. goto out_unlock;
  808. }
  809. new_size = i_size_read(inode) - len;
  810. error = xfs_collapse_file_space(ip, offset, len);
  811. if (error)
  812. goto out_unlock;
  813. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  814. unsigned blksize_mask = (1 << inode->i_blkbits) - 1;
  815. new_size = i_size_read(inode) + len;
  816. if (offset & blksize_mask || len & blksize_mask) {
  817. error = -EINVAL;
  818. goto out_unlock;
  819. }
  820. /* check the new inode size does not wrap through zero */
  821. if (new_size > inode->i_sb->s_maxbytes) {
  822. error = -EFBIG;
  823. goto out_unlock;
  824. }
  825. /* Offset should be less than i_size */
  826. if (offset >= i_size_read(inode)) {
  827. error = -EINVAL;
  828. goto out_unlock;
  829. }
  830. do_file_insert = 1;
  831. } else {
  832. flags |= XFS_PREALLOC_SET;
  833. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  834. offset + len > i_size_read(inode)) {
  835. new_size = offset + len;
  836. error = inode_newsize_ok(inode, new_size);
  837. if (error)
  838. goto out_unlock;
  839. }
  840. if (mode & FALLOC_FL_ZERO_RANGE)
  841. error = xfs_zero_file_space(ip, offset, len);
  842. else
  843. error = xfs_alloc_file_space(ip, offset, len,
  844. XFS_BMAPI_PREALLOC);
  845. if (error)
  846. goto out_unlock;
  847. }
  848. if (file->f_flags & O_DSYNC)
  849. flags |= XFS_PREALLOC_SYNC;
  850. error = xfs_update_prealloc_flags(ip, flags);
  851. if (error)
  852. goto out_unlock;
  853. /* Change file size if needed */
  854. if (new_size) {
  855. struct iattr iattr;
  856. iattr.ia_valid = ATTR_SIZE;
  857. iattr.ia_size = new_size;
  858. error = xfs_setattr_size(ip, &iattr);
  859. if (error)
  860. goto out_unlock;
  861. }
  862. /*
  863. * Perform hole insertion now that the file size has been
  864. * updated so that if we crash during the operation we don't
  865. * leave shifted extents past EOF and hence losing access to
  866. * the data that is contained within them.
  867. */
  868. if (do_file_insert)
  869. error = xfs_insert_file_space(ip, offset, len);
  870. out_unlock:
  871. xfs_iunlock(ip, iolock);
  872. return error;
  873. }
  874. STATIC int
  875. xfs_file_open(
  876. struct inode *inode,
  877. struct file *file)
  878. {
  879. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  880. return -EFBIG;
  881. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  882. return -EIO;
  883. return 0;
  884. }
  885. STATIC int
  886. xfs_dir_open(
  887. struct inode *inode,
  888. struct file *file)
  889. {
  890. struct xfs_inode *ip = XFS_I(inode);
  891. int mode;
  892. int error;
  893. error = xfs_file_open(inode, file);
  894. if (error)
  895. return error;
  896. /*
  897. * If there are any blocks, read-ahead block 0 as we're almost
  898. * certain to have the next operation be a read there.
  899. */
  900. mode = xfs_ilock_data_map_shared(ip);
  901. if (ip->i_d.di_nextents > 0)
  902. xfs_dir3_data_readahead(ip, 0, -1);
  903. xfs_iunlock(ip, mode);
  904. return 0;
  905. }
  906. STATIC int
  907. xfs_file_release(
  908. struct inode *inode,
  909. struct file *filp)
  910. {
  911. return xfs_release(XFS_I(inode));
  912. }
  913. STATIC int
  914. xfs_file_readdir(
  915. struct file *file,
  916. struct dir_context *ctx)
  917. {
  918. struct inode *inode = file_inode(file);
  919. xfs_inode_t *ip = XFS_I(inode);
  920. size_t bufsize;
  921. /*
  922. * The Linux API doesn't pass down the total size of the buffer
  923. * we read into down to the filesystem. With the filldir concept
  924. * it's not needed for correct information, but the XFS dir2 leaf
  925. * code wants an estimate of the buffer size to calculate it's
  926. * readahead window and size the buffers used for mapping to
  927. * physical blocks.
  928. *
  929. * Try to give it an estimate that's good enough, maybe at some
  930. * point we can change the ->readdir prototype to include the
  931. * buffer size. For now we use the current glibc buffer size.
  932. */
  933. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  934. return xfs_readdir(ip, ctx, bufsize);
  935. }
  936. STATIC int
  937. xfs_file_mmap(
  938. struct file *filp,
  939. struct vm_area_struct *vma)
  940. {
  941. vma->vm_ops = &xfs_file_vm_ops;
  942. file_accessed(filp);
  943. return 0;
  944. }
  945. /*
  946. * This type is designed to indicate the type of offset we would like
  947. * to search from page cache for xfs_seek_hole_data().
  948. */
  949. enum {
  950. HOLE_OFF = 0,
  951. DATA_OFF,
  952. };
  953. /*
  954. * Lookup the desired type of offset from the given page.
  955. *
  956. * On success, return true and the offset argument will point to the
  957. * start of the region that was found. Otherwise this function will
  958. * return false and keep the offset argument unchanged.
  959. */
  960. STATIC bool
  961. xfs_lookup_buffer_offset(
  962. struct page *page,
  963. loff_t *offset,
  964. unsigned int type)
  965. {
  966. loff_t lastoff = page_offset(page);
  967. bool found = false;
  968. struct buffer_head *bh, *head;
  969. bh = head = page_buffers(page);
  970. do {
  971. /*
  972. * Unwritten extents that have data in the page
  973. * cache covering them can be identified by the
  974. * BH_Unwritten state flag. Pages with multiple
  975. * buffers might have a mix of holes, data and
  976. * unwritten extents - any buffer with valid
  977. * data in it should have BH_Uptodate flag set
  978. * on it.
  979. */
  980. if (buffer_unwritten(bh) ||
  981. buffer_uptodate(bh)) {
  982. if (type == DATA_OFF)
  983. found = true;
  984. } else {
  985. if (type == HOLE_OFF)
  986. found = true;
  987. }
  988. if (found) {
  989. *offset = lastoff;
  990. break;
  991. }
  992. lastoff += bh->b_size;
  993. } while ((bh = bh->b_this_page) != head);
  994. return found;
  995. }
  996. /*
  997. * This routine is called to find out and return a data or hole offset
  998. * from the page cache for unwritten extents according to the desired
  999. * type for xfs_seek_hole_data().
  1000. *
  1001. * The argument offset is used to tell where we start to search from the
  1002. * page cache. Map is used to figure out the end points of the range to
  1003. * lookup pages.
  1004. *
  1005. * Return true if the desired type of offset was found, and the argument
  1006. * offset is filled with that address. Otherwise, return false and keep
  1007. * offset unchanged.
  1008. */
  1009. STATIC bool
  1010. xfs_find_get_desired_pgoff(
  1011. struct inode *inode,
  1012. struct xfs_bmbt_irec *map,
  1013. unsigned int type,
  1014. loff_t *offset)
  1015. {
  1016. struct xfs_inode *ip = XFS_I(inode);
  1017. struct xfs_mount *mp = ip->i_mount;
  1018. struct pagevec pvec;
  1019. pgoff_t index;
  1020. pgoff_t end;
  1021. loff_t endoff;
  1022. loff_t startoff = *offset;
  1023. loff_t lastoff = startoff;
  1024. bool found = false;
  1025. pagevec_init(&pvec, 0);
  1026. index = startoff >> PAGE_CACHE_SHIFT;
  1027. endoff = XFS_FSB_TO_B(mp, map->br_startoff + map->br_blockcount);
  1028. end = endoff >> PAGE_CACHE_SHIFT;
  1029. do {
  1030. int want;
  1031. unsigned nr_pages;
  1032. unsigned int i;
  1033. want = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
  1034. nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
  1035. want);
  1036. /*
  1037. * No page mapped into given range. If we are searching holes
  1038. * and if this is the first time we got into the loop, it means
  1039. * that the given offset is landed in a hole, return it.
  1040. *
  1041. * If we have already stepped through some block buffers to find
  1042. * holes but they all contains data. In this case, the last
  1043. * offset is already updated and pointed to the end of the last
  1044. * mapped page, if it does not reach the endpoint to search,
  1045. * that means there should be a hole between them.
  1046. */
  1047. if (nr_pages == 0) {
  1048. /* Data search found nothing */
  1049. if (type == DATA_OFF)
  1050. break;
  1051. ASSERT(type == HOLE_OFF);
  1052. if (lastoff == startoff || lastoff < endoff) {
  1053. found = true;
  1054. *offset = lastoff;
  1055. }
  1056. break;
  1057. }
  1058. /*
  1059. * At lease we found one page. If this is the first time we
  1060. * step into the loop, and if the first page index offset is
  1061. * greater than the given search offset, a hole was found.
  1062. */
  1063. if (type == HOLE_OFF && lastoff == startoff &&
  1064. lastoff < page_offset(pvec.pages[0])) {
  1065. found = true;
  1066. break;
  1067. }
  1068. for (i = 0; i < nr_pages; i++) {
  1069. struct page *page = pvec.pages[i];
  1070. loff_t b_offset;
  1071. /*
  1072. * At this point, the page may be truncated or
  1073. * invalidated (changing page->mapping to NULL),
  1074. * or even swizzled back from swapper_space to tmpfs
  1075. * file mapping. However, page->index will not change
  1076. * because we have a reference on the page.
  1077. *
  1078. * Searching done if the page index is out of range.
  1079. * If the current offset is not reaches the end of
  1080. * the specified search range, there should be a hole
  1081. * between them.
  1082. */
  1083. if (page->index > end) {
  1084. if (type == HOLE_OFF && lastoff < endoff) {
  1085. *offset = lastoff;
  1086. found = true;
  1087. }
  1088. goto out;
  1089. }
  1090. lock_page(page);
  1091. /*
  1092. * Page truncated or invalidated(page->mapping == NULL).
  1093. * We can freely skip it and proceed to check the next
  1094. * page.
  1095. */
  1096. if (unlikely(page->mapping != inode->i_mapping)) {
  1097. unlock_page(page);
  1098. continue;
  1099. }
  1100. if (!page_has_buffers(page)) {
  1101. unlock_page(page);
  1102. continue;
  1103. }
  1104. found = xfs_lookup_buffer_offset(page, &b_offset, type);
  1105. if (found) {
  1106. /*
  1107. * The found offset may be less than the start
  1108. * point to search if this is the first time to
  1109. * come here.
  1110. */
  1111. *offset = max_t(loff_t, startoff, b_offset);
  1112. unlock_page(page);
  1113. goto out;
  1114. }
  1115. /*
  1116. * We either searching data but nothing was found, or
  1117. * searching hole but found a data buffer. In either
  1118. * case, probably the next page contains the desired
  1119. * things, update the last offset to it so.
  1120. */
  1121. lastoff = page_offset(page) + PAGE_SIZE;
  1122. unlock_page(page);
  1123. }
  1124. /*
  1125. * The number of returned pages less than our desired, search
  1126. * done. In this case, nothing was found for searching data,
  1127. * but we found a hole behind the last offset.
  1128. */
  1129. if (nr_pages < want) {
  1130. if (type == HOLE_OFF) {
  1131. *offset = lastoff;
  1132. found = true;
  1133. }
  1134. break;
  1135. }
  1136. index = pvec.pages[i - 1]->index + 1;
  1137. pagevec_release(&pvec);
  1138. } while (index <= end);
  1139. out:
  1140. pagevec_release(&pvec);
  1141. return found;
  1142. }
  1143. STATIC loff_t
  1144. xfs_seek_hole_data(
  1145. struct file *file,
  1146. loff_t start,
  1147. int whence)
  1148. {
  1149. struct inode *inode = file->f_mapping->host;
  1150. struct xfs_inode *ip = XFS_I(inode);
  1151. struct xfs_mount *mp = ip->i_mount;
  1152. loff_t uninitialized_var(offset);
  1153. xfs_fsize_t isize;
  1154. xfs_fileoff_t fsbno;
  1155. xfs_filblks_t end;
  1156. uint lock;
  1157. int error;
  1158. if (XFS_FORCED_SHUTDOWN(mp))
  1159. return -EIO;
  1160. lock = xfs_ilock_data_map_shared(ip);
  1161. isize = i_size_read(inode);
  1162. if (start >= isize) {
  1163. error = -ENXIO;
  1164. goto out_unlock;
  1165. }
  1166. /*
  1167. * Try to read extents from the first block indicated
  1168. * by fsbno to the end block of the file.
  1169. */
  1170. fsbno = XFS_B_TO_FSBT(mp, start);
  1171. end = XFS_B_TO_FSB(mp, isize);
  1172. for (;;) {
  1173. struct xfs_bmbt_irec map[2];
  1174. int nmap = 2;
  1175. unsigned int i;
  1176. error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
  1177. XFS_BMAPI_ENTIRE);
  1178. if (error)
  1179. goto out_unlock;
  1180. /* No extents at given offset, must be beyond EOF */
  1181. if (nmap == 0) {
  1182. error = -ENXIO;
  1183. goto out_unlock;
  1184. }
  1185. for (i = 0; i < nmap; i++) {
  1186. offset = max_t(loff_t, start,
  1187. XFS_FSB_TO_B(mp, map[i].br_startoff));
  1188. /* Landed in the hole we wanted? */
  1189. if (whence == SEEK_HOLE &&
  1190. map[i].br_startblock == HOLESTARTBLOCK)
  1191. goto out;
  1192. /* Landed in the data extent we wanted? */
  1193. if (whence == SEEK_DATA &&
  1194. (map[i].br_startblock == DELAYSTARTBLOCK ||
  1195. (map[i].br_state == XFS_EXT_NORM &&
  1196. !isnullstartblock(map[i].br_startblock))))
  1197. goto out;
  1198. /*
  1199. * Landed in an unwritten extent, try to search
  1200. * for hole or data from page cache.
  1201. */
  1202. if (map[i].br_state == XFS_EXT_UNWRITTEN) {
  1203. if (xfs_find_get_desired_pgoff(inode, &map[i],
  1204. whence == SEEK_HOLE ? HOLE_OFF : DATA_OFF,
  1205. &offset))
  1206. goto out;
  1207. }
  1208. }
  1209. /*
  1210. * We only received one extent out of the two requested. This
  1211. * means we've hit EOF and didn't find what we are looking for.
  1212. */
  1213. if (nmap == 1) {
  1214. /*
  1215. * If we were looking for a hole, set offset to
  1216. * the end of the file (i.e., there is an implicit
  1217. * hole at the end of any file).
  1218. */
  1219. if (whence == SEEK_HOLE) {
  1220. offset = isize;
  1221. break;
  1222. }
  1223. /*
  1224. * If we were looking for data, it's nowhere to be found
  1225. */
  1226. ASSERT(whence == SEEK_DATA);
  1227. error = -ENXIO;
  1228. goto out_unlock;
  1229. }
  1230. ASSERT(i > 1);
  1231. /*
  1232. * Nothing was found, proceed to the next round of search
  1233. * if the next reading offset is not at or beyond EOF.
  1234. */
  1235. fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
  1236. start = XFS_FSB_TO_B(mp, fsbno);
  1237. if (start >= isize) {
  1238. if (whence == SEEK_HOLE) {
  1239. offset = isize;
  1240. break;
  1241. }
  1242. ASSERT(whence == SEEK_DATA);
  1243. error = -ENXIO;
  1244. goto out_unlock;
  1245. }
  1246. }
  1247. out:
  1248. /*
  1249. * If at this point we have found the hole we wanted, the returned
  1250. * offset may be bigger than the file size as it may be aligned to
  1251. * page boundary for unwritten extents. We need to deal with this
  1252. * situation in particular.
  1253. */
  1254. if (whence == SEEK_HOLE)
  1255. offset = min_t(loff_t, offset, isize);
  1256. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  1257. out_unlock:
  1258. xfs_iunlock(ip, lock);
  1259. if (error)
  1260. return error;
  1261. return offset;
  1262. }
  1263. STATIC loff_t
  1264. xfs_file_llseek(
  1265. struct file *file,
  1266. loff_t offset,
  1267. int whence)
  1268. {
  1269. switch (whence) {
  1270. case SEEK_END:
  1271. case SEEK_CUR:
  1272. case SEEK_SET:
  1273. return generic_file_llseek(file, offset, whence);
  1274. case SEEK_HOLE:
  1275. case SEEK_DATA:
  1276. return xfs_seek_hole_data(file, offset, whence);
  1277. default:
  1278. return -EINVAL;
  1279. }
  1280. }
  1281. /*
  1282. * Locking for serialisation of IO during page faults. This results in a lock
  1283. * ordering of:
  1284. *
  1285. * mmap_sem (MM)
  1286. * i_mmap_lock (XFS - truncate serialisation)
  1287. * page_lock (MM)
  1288. * i_lock (XFS - extent map serialisation)
  1289. */
  1290. STATIC int
  1291. xfs_filemap_fault(
  1292. struct vm_area_struct *vma,
  1293. struct vm_fault *vmf)
  1294. {
  1295. struct xfs_inode *ip = XFS_I(vma->vm_file->f_mapping->host);
  1296. int error;
  1297. trace_xfs_filemap_fault(ip);
  1298. xfs_ilock(ip, XFS_MMAPLOCK_SHARED);
  1299. error = filemap_fault(vma, vmf);
  1300. xfs_iunlock(ip, XFS_MMAPLOCK_SHARED);
  1301. return error;
  1302. }
  1303. /*
  1304. * mmap()d file has taken write protection fault and is being made writable. We
  1305. * can set the page state up correctly for a writable page, which means we can
  1306. * do correct delalloc accounting (ENOSPC checking!) and unwritten extent
  1307. * mapping.
  1308. */
  1309. STATIC int
  1310. xfs_filemap_page_mkwrite(
  1311. struct vm_area_struct *vma,
  1312. struct vm_fault *vmf)
  1313. {
  1314. struct xfs_inode *ip = XFS_I(vma->vm_file->f_mapping->host);
  1315. int error;
  1316. trace_xfs_filemap_page_mkwrite(ip);
  1317. xfs_ilock(ip, XFS_MMAPLOCK_SHARED);
  1318. error = block_page_mkwrite(vma, vmf, xfs_get_blocks);
  1319. xfs_iunlock(ip, XFS_MMAPLOCK_SHARED);
  1320. return error;
  1321. }
  1322. const struct file_operations xfs_file_operations = {
  1323. .llseek = xfs_file_llseek,
  1324. .read_iter = xfs_file_read_iter,
  1325. .write_iter = xfs_file_write_iter,
  1326. .splice_read = xfs_file_splice_read,
  1327. .splice_write = iter_file_splice_write,
  1328. .unlocked_ioctl = xfs_file_ioctl,
  1329. #ifdef CONFIG_COMPAT
  1330. .compat_ioctl = xfs_file_compat_ioctl,
  1331. #endif
  1332. .mmap = xfs_file_mmap,
  1333. .open = xfs_file_open,
  1334. .release = xfs_file_release,
  1335. .fsync = xfs_file_fsync,
  1336. .fallocate = xfs_file_fallocate,
  1337. };
  1338. const struct file_operations xfs_dir_file_operations = {
  1339. .open = xfs_dir_open,
  1340. .read = generic_read_dir,
  1341. .iterate = xfs_file_readdir,
  1342. .llseek = generic_file_llseek,
  1343. .unlocked_ioctl = xfs_file_ioctl,
  1344. #ifdef CONFIG_COMPAT
  1345. .compat_ioctl = xfs_file_compat_ioctl,
  1346. #endif
  1347. .fsync = xfs_dir_fsync,
  1348. };
  1349. static const struct vm_operations_struct xfs_file_vm_ops = {
  1350. .fault = xfs_filemap_fault,
  1351. .map_pages = filemap_map_pages,
  1352. .page_mkwrite = xfs_filemap_page_mkwrite,
  1353. };