xfs_file.c 37 KB

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