xfs_file.c 36 KB

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