file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * linux/fs/ext4/file.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/file.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * ext4 fs regular file handling primitives
  16. *
  17. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  18. * (jj@sunsite.ms.mff.cuni.cz)
  19. */
  20. #include <linux/time.h>
  21. #include <linux/fs.h>
  22. #include <linux/jbd2.h>
  23. #include <linux/mount.h>
  24. #include <linux/path.h>
  25. #include <linux/aio.h>
  26. #include <linux/quotaops.h>
  27. #include <linux/pagevec.h>
  28. #include "ext4.h"
  29. #include "ext4_jbd2.h"
  30. #include "xattr.h"
  31. #include "acl.h"
  32. /*
  33. * Called when an inode is released. Note that this is different
  34. * from ext4_file_open: open gets called at every open, but release
  35. * gets called only when /all/ the files are closed.
  36. */
  37. static int ext4_release_file(struct inode *inode, struct file *filp)
  38. {
  39. if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
  40. ext4_alloc_da_blocks(inode);
  41. ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  42. }
  43. /* if we are the last writer on the inode, drop the block reservation */
  44. if ((filp->f_mode & FMODE_WRITE) &&
  45. (atomic_read(&inode->i_writecount) == 1) &&
  46. !EXT4_I(inode)->i_reserved_data_blocks)
  47. {
  48. down_write(&EXT4_I(inode)->i_data_sem);
  49. ext4_discard_preallocations(inode);
  50. up_write(&EXT4_I(inode)->i_data_sem);
  51. }
  52. if (is_dx(inode) && filp->private_data)
  53. ext4_htree_free_dir_info(filp->private_data);
  54. return 0;
  55. }
  56. static void ext4_unwritten_wait(struct inode *inode)
  57. {
  58. wait_queue_head_t *wq = ext4_ioend_wq(inode);
  59. wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_unwritten) == 0));
  60. }
  61. /*
  62. * This tests whether the IO in question is block-aligned or not.
  63. * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
  64. * are converted to written only after the IO is complete. Until they are
  65. * mapped, these blocks appear as holes, so dio_zero_block() will assume that
  66. * it needs to zero out portions of the start and/or end block. If 2 AIO
  67. * threads are at work on the same unwritten block, they must be synchronized
  68. * or one thread will zero the other's data, causing corruption.
  69. */
  70. static int
  71. ext4_unaligned_aio(struct inode *inode, struct iov_iter *from, loff_t pos)
  72. {
  73. struct super_block *sb = inode->i_sb;
  74. int blockmask = sb->s_blocksize - 1;
  75. if (pos >= i_size_read(inode))
  76. return 0;
  77. if ((pos | iov_iter_alignment(from)) & blockmask)
  78. return 1;
  79. return 0;
  80. }
  81. static ssize_t
  82. ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  83. {
  84. struct file *file = iocb->ki_filp;
  85. struct inode *inode = file_inode(iocb->ki_filp);
  86. struct mutex *aio_mutex = NULL;
  87. struct blk_plug plug;
  88. int o_direct = file->f_flags & O_DIRECT;
  89. int overwrite = 0;
  90. size_t length = iov_iter_count(from);
  91. ssize_t ret;
  92. loff_t pos = iocb->ki_pos;
  93. /*
  94. * Unaligned direct AIO must be serialized; see comment above
  95. * In the case of O_APPEND, assume that we must always serialize
  96. */
  97. if (o_direct &&
  98. ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
  99. !is_sync_kiocb(iocb) &&
  100. (file->f_flags & O_APPEND ||
  101. ext4_unaligned_aio(inode, from, pos))) {
  102. aio_mutex = ext4_aio_mutex(inode);
  103. mutex_lock(aio_mutex);
  104. ext4_unwritten_wait(inode);
  105. }
  106. mutex_lock(&inode->i_mutex);
  107. if (file->f_flags & O_APPEND)
  108. iocb->ki_pos = pos = i_size_read(inode);
  109. /*
  110. * If we have encountered a bitmap-format file, the size limit
  111. * is smaller than s_maxbytes, which is for extent-mapped files.
  112. */
  113. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  114. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  115. if ((pos > sbi->s_bitmap_maxbytes) ||
  116. (pos == sbi->s_bitmap_maxbytes && length > 0)) {
  117. mutex_unlock(&inode->i_mutex);
  118. ret = -EFBIG;
  119. goto errout;
  120. }
  121. if (pos + length > sbi->s_bitmap_maxbytes)
  122. iov_iter_truncate(from, sbi->s_bitmap_maxbytes - pos);
  123. }
  124. iocb->private = &overwrite;
  125. if (o_direct) {
  126. blk_start_plug(&plug);
  127. /* check whether we do a DIO overwrite or not */
  128. if (ext4_should_dioread_nolock(inode) && !aio_mutex &&
  129. !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) {
  130. struct ext4_map_blocks map;
  131. unsigned int blkbits = inode->i_blkbits;
  132. int err, len;
  133. map.m_lblk = pos >> blkbits;
  134. map.m_len = (EXT4_BLOCK_ALIGN(pos + length, blkbits) >> blkbits)
  135. - map.m_lblk;
  136. len = map.m_len;
  137. err = ext4_map_blocks(NULL, inode, &map, 0);
  138. /*
  139. * 'err==len' means that all of blocks has
  140. * been preallocated no matter they are
  141. * initialized or not. For excluding
  142. * unwritten extents, we need to check
  143. * m_flags. There are two conditions that
  144. * indicate for initialized extents. 1) If we
  145. * hit extent cache, EXT4_MAP_MAPPED flag is
  146. * returned; 2) If we do a real lookup,
  147. * non-flags are returned. So we should check
  148. * these two conditions.
  149. */
  150. if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
  151. overwrite = 1;
  152. }
  153. }
  154. ret = __generic_file_write_iter(iocb, from);
  155. mutex_unlock(&inode->i_mutex);
  156. if (ret > 0) {
  157. ssize_t err;
  158. err = generic_write_sync(file, iocb->ki_pos - ret, ret);
  159. if (err < 0)
  160. ret = err;
  161. }
  162. if (o_direct)
  163. blk_finish_plug(&plug);
  164. errout:
  165. if (aio_mutex)
  166. mutex_unlock(aio_mutex);
  167. return ret;
  168. }
  169. static const struct vm_operations_struct ext4_file_vm_ops = {
  170. .fault = filemap_fault,
  171. .map_pages = filemap_map_pages,
  172. .page_mkwrite = ext4_page_mkwrite,
  173. .remap_pages = generic_file_remap_pages,
  174. };
  175. static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
  176. {
  177. file_accessed(file);
  178. vma->vm_ops = &ext4_file_vm_ops;
  179. return 0;
  180. }
  181. static int ext4_file_open(struct inode * inode, struct file * filp)
  182. {
  183. struct super_block *sb = inode->i_sb;
  184. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  185. struct vfsmount *mnt = filp->f_path.mnt;
  186. struct path path;
  187. char buf[64], *cp;
  188. if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
  189. !(sb->s_flags & MS_RDONLY))) {
  190. sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
  191. /*
  192. * Sample where the filesystem has been mounted and
  193. * store it in the superblock for sysadmin convenience
  194. * when trying to sort through large numbers of block
  195. * devices or filesystem images.
  196. */
  197. memset(buf, 0, sizeof(buf));
  198. path.mnt = mnt;
  199. path.dentry = mnt->mnt_root;
  200. cp = d_path(&path, buf, sizeof(buf));
  201. if (!IS_ERR(cp)) {
  202. handle_t *handle;
  203. int err;
  204. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  205. if (IS_ERR(handle))
  206. return PTR_ERR(handle);
  207. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  208. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  209. if (err) {
  210. ext4_journal_stop(handle);
  211. return err;
  212. }
  213. strlcpy(sbi->s_es->s_last_mounted, cp,
  214. sizeof(sbi->s_es->s_last_mounted));
  215. ext4_handle_dirty_super(handle, sb);
  216. ext4_journal_stop(handle);
  217. }
  218. }
  219. /*
  220. * Set up the jbd2_inode if we are opening the inode for
  221. * writing and the journal is present
  222. */
  223. if (filp->f_mode & FMODE_WRITE) {
  224. int ret = ext4_inode_attach_jinode(inode);
  225. if (ret < 0)
  226. return ret;
  227. }
  228. return dquot_file_open(inode, filp);
  229. }
  230. /*
  231. * Here we use ext4_map_blocks() to get a block mapping for a extent-based
  232. * file rather than ext4_ext_walk_space() because we can introduce
  233. * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
  234. * function. When extent status tree has been fully implemented, it will
  235. * track all extent status for a file and we can directly use it to
  236. * retrieve the offset for SEEK_DATA/SEEK_HOLE.
  237. */
  238. /*
  239. * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
  240. * lookup page cache to check whether or not there has some data between
  241. * [startoff, endoff] because, if this range contains an unwritten extent,
  242. * we determine this extent as a data or a hole according to whether the
  243. * page cache has data or not.
  244. */
  245. static int ext4_find_unwritten_pgoff(struct inode *inode, int whence,
  246. loff_t endoff, loff_t *offset)
  247. {
  248. struct pagevec pvec;
  249. pgoff_t index;
  250. pgoff_t end;
  251. loff_t startoff;
  252. loff_t lastoff;
  253. int found = 0;
  254. startoff = *offset;
  255. lastoff = startoff;
  256. index = startoff >> PAGE_CACHE_SHIFT;
  257. end = endoff >> PAGE_CACHE_SHIFT;
  258. pagevec_init(&pvec, 0);
  259. do {
  260. int i, num;
  261. unsigned long nr_pages;
  262. num = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
  263. nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
  264. (pgoff_t)num);
  265. if (nr_pages == 0) {
  266. if (whence == SEEK_DATA)
  267. break;
  268. BUG_ON(whence != SEEK_HOLE);
  269. /*
  270. * If this is the first time to go into the loop and
  271. * offset is not beyond the end offset, it will be a
  272. * hole at this offset
  273. */
  274. if (lastoff == startoff || lastoff < endoff)
  275. found = 1;
  276. break;
  277. }
  278. /*
  279. * If this is the first time to go into the loop and
  280. * offset is smaller than the first page offset, it will be a
  281. * hole at this offset.
  282. */
  283. if (lastoff == startoff && whence == SEEK_HOLE &&
  284. lastoff < page_offset(pvec.pages[0])) {
  285. found = 1;
  286. break;
  287. }
  288. for (i = 0; i < nr_pages; i++) {
  289. struct page *page = pvec.pages[i];
  290. struct buffer_head *bh, *head;
  291. /*
  292. * If the current offset is not beyond the end of given
  293. * range, it will be a hole.
  294. */
  295. if (lastoff < endoff && whence == SEEK_HOLE &&
  296. page->index > end) {
  297. found = 1;
  298. *offset = lastoff;
  299. goto out;
  300. }
  301. lock_page(page);
  302. if (unlikely(page->mapping != inode->i_mapping)) {
  303. unlock_page(page);
  304. continue;
  305. }
  306. if (!page_has_buffers(page)) {
  307. unlock_page(page);
  308. continue;
  309. }
  310. if (page_has_buffers(page)) {
  311. lastoff = page_offset(page);
  312. bh = head = page_buffers(page);
  313. do {
  314. if (buffer_uptodate(bh) ||
  315. buffer_unwritten(bh)) {
  316. if (whence == SEEK_DATA)
  317. found = 1;
  318. } else {
  319. if (whence == SEEK_HOLE)
  320. found = 1;
  321. }
  322. if (found) {
  323. *offset = max_t(loff_t,
  324. startoff, lastoff);
  325. unlock_page(page);
  326. goto out;
  327. }
  328. lastoff += bh->b_size;
  329. bh = bh->b_this_page;
  330. } while (bh != head);
  331. }
  332. lastoff = page_offset(page) + PAGE_SIZE;
  333. unlock_page(page);
  334. }
  335. /*
  336. * The no. of pages is less than our desired, that would be a
  337. * hole in there.
  338. */
  339. if (nr_pages < num && whence == SEEK_HOLE) {
  340. found = 1;
  341. *offset = lastoff;
  342. break;
  343. }
  344. index = pvec.pages[i - 1]->index + 1;
  345. pagevec_release(&pvec);
  346. } while (index <= end);
  347. out:
  348. pagevec_release(&pvec);
  349. return found;
  350. }
  351. /*
  352. * ext4_seek_data() retrieves the offset for SEEK_DATA.
  353. */
  354. static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
  355. {
  356. struct inode *inode = file->f_mapping->host;
  357. struct fiemap_extent_info fie;
  358. struct fiemap_extent ext[2];
  359. loff_t next;
  360. int i, ret = 0;
  361. mutex_lock(&inode->i_mutex);
  362. if (offset >= inode->i_size) {
  363. mutex_unlock(&inode->i_mutex);
  364. return -ENXIO;
  365. }
  366. fie.fi_flags = 0;
  367. fie.fi_extents_max = 2;
  368. fie.fi_extents_start = (struct fiemap_extent __user *) &ext;
  369. while (1) {
  370. mm_segment_t old_fs = get_fs();
  371. fie.fi_extents_mapped = 0;
  372. memset(ext, 0, sizeof(*ext) * fie.fi_extents_max);
  373. set_fs(get_ds());
  374. ret = ext4_fiemap(inode, &fie, offset, maxsize - offset);
  375. set_fs(old_fs);
  376. if (ret)
  377. break;
  378. /* No extents found, EOF */
  379. if (!fie.fi_extents_mapped) {
  380. ret = -ENXIO;
  381. break;
  382. }
  383. for (i = 0; i < fie.fi_extents_mapped; i++) {
  384. next = (loff_t)(ext[i].fe_length + ext[i].fe_logical);
  385. if (offset < (loff_t)ext[i].fe_logical)
  386. offset = (loff_t)ext[i].fe_logical;
  387. /*
  388. * If extent is not unwritten, then it contains valid
  389. * data, mapped or delayed.
  390. */
  391. if (!(ext[i].fe_flags & FIEMAP_EXTENT_UNWRITTEN))
  392. goto out;
  393. /*
  394. * If there is a unwritten extent at this offset,
  395. * it will be as a data or a hole according to page
  396. * cache that has data or not.
  397. */
  398. if (ext4_find_unwritten_pgoff(inode, SEEK_DATA,
  399. next, &offset))
  400. goto out;
  401. if (ext[i].fe_flags & FIEMAP_EXTENT_LAST) {
  402. ret = -ENXIO;
  403. goto out;
  404. }
  405. offset = next;
  406. }
  407. }
  408. if (offset > inode->i_size)
  409. offset = inode->i_size;
  410. out:
  411. mutex_unlock(&inode->i_mutex);
  412. if (ret)
  413. return ret;
  414. return vfs_setpos(file, offset, maxsize);
  415. }
  416. /*
  417. * ext4_seek_hole() retrieves the offset for SEEK_HOLE
  418. */
  419. static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
  420. {
  421. struct inode *inode = file->f_mapping->host;
  422. struct fiemap_extent_info fie;
  423. struct fiemap_extent ext[2];
  424. loff_t next;
  425. int i, ret = 0;
  426. mutex_lock(&inode->i_mutex);
  427. if (offset >= inode->i_size) {
  428. mutex_unlock(&inode->i_mutex);
  429. return -ENXIO;
  430. }
  431. fie.fi_flags = 0;
  432. fie.fi_extents_max = 2;
  433. fie.fi_extents_start = (struct fiemap_extent __user *)&ext;
  434. while (1) {
  435. mm_segment_t old_fs = get_fs();
  436. fie.fi_extents_mapped = 0;
  437. memset(ext, 0, sizeof(*ext));
  438. set_fs(get_ds());
  439. ret = ext4_fiemap(inode, &fie, offset, maxsize - offset);
  440. set_fs(old_fs);
  441. if (ret)
  442. break;
  443. /* No extents found */
  444. if (!fie.fi_extents_mapped)
  445. break;
  446. for (i = 0; i < fie.fi_extents_mapped; i++) {
  447. next = (loff_t)(ext[i].fe_logical + ext[i].fe_length);
  448. /*
  449. * If extent is not unwritten, then it contains valid
  450. * data, mapped or delayed.
  451. */
  452. if (!(ext[i].fe_flags & FIEMAP_EXTENT_UNWRITTEN)) {
  453. if (offset < (loff_t)ext[i].fe_logical)
  454. goto out;
  455. offset = next;
  456. continue;
  457. }
  458. /*
  459. * If there is a unwritten extent at this offset,
  460. * it will be as a data or a hole according to page
  461. * cache that has data or not.
  462. */
  463. if (ext4_find_unwritten_pgoff(inode, SEEK_HOLE,
  464. next, &offset))
  465. goto out;
  466. offset = next;
  467. if (ext[i].fe_flags & FIEMAP_EXTENT_LAST)
  468. goto out;
  469. }
  470. }
  471. if (offset > inode->i_size)
  472. offset = inode->i_size;
  473. out:
  474. mutex_unlock(&inode->i_mutex);
  475. if (ret)
  476. return ret;
  477. return vfs_setpos(file, offset, maxsize);
  478. }
  479. /*
  480. * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
  481. * by calling generic_file_llseek_size() with the appropriate maxbytes
  482. * value for each.
  483. */
  484. loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
  485. {
  486. struct inode *inode = file->f_mapping->host;
  487. loff_t maxbytes;
  488. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  489. maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  490. else
  491. maxbytes = inode->i_sb->s_maxbytes;
  492. switch (whence) {
  493. case SEEK_SET:
  494. case SEEK_CUR:
  495. case SEEK_END:
  496. return generic_file_llseek_size(file, offset, whence,
  497. maxbytes, i_size_read(inode));
  498. case SEEK_DATA:
  499. return ext4_seek_data(file, offset, maxbytes);
  500. case SEEK_HOLE:
  501. return ext4_seek_hole(file, offset, maxbytes);
  502. }
  503. return -EINVAL;
  504. }
  505. const struct file_operations ext4_file_operations = {
  506. .llseek = ext4_llseek,
  507. .read = new_sync_read,
  508. .write = new_sync_write,
  509. .read_iter = generic_file_read_iter,
  510. .write_iter = ext4_file_write_iter,
  511. .unlocked_ioctl = ext4_ioctl,
  512. #ifdef CONFIG_COMPAT
  513. .compat_ioctl = ext4_compat_ioctl,
  514. #endif
  515. .mmap = ext4_file_mmap,
  516. .open = ext4_file_open,
  517. .release = ext4_release_file,
  518. .fsync = ext4_sync_file,
  519. .splice_read = generic_file_splice_read,
  520. .splice_write = iter_file_splice_write,
  521. .fallocate = ext4_fallocate,
  522. };
  523. const struct inode_operations ext4_file_inode_operations = {
  524. .setattr = ext4_setattr,
  525. .getattr = ext4_getattr,
  526. .setxattr = generic_setxattr,
  527. .getxattr = generic_getxattr,
  528. .listxattr = ext4_listxattr,
  529. .removexattr = generic_removexattr,
  530. .get_acl = ext4_get_acl,
  531. .set_acl = ext4_set_acl,
  532. .fiemap = ext4_fiemap,
  533. };