xfs_file.c 36 KB

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