xfs_file.c 36 KB

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