file.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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, const struct iovec *iov,
  72. unsigned long nr_segs, loff_t pos)
  73. {
  74. struct super_block *sb = inode->i_sb;
  75. int blockmask = sb->s_blocksize - 1;
  76. size_t count = iov_length(iov, nr_segs);
  77. loff_t final_size = pos + count;
  78. if (pos >= i_size_read(inode))
  79. return 0;
  80. if ((pos & blockmask) || (final_size & blockmask))
  81. return 1;
  82. return 0;
  83. }
  84. static ssize_t
  85. ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
  86. unsigned long nr_segs, loff_t pos)
  87. {
  88. struct file *file = iocb->ki_filp;
  89. struct inode *inode = file_inode(iocb->ki_filp);
  90. struct mutex *aio_mutex = NULL;
  91. struct blk_plug plug;
  92. int o_direct = file->f_flags & O_DIRECT;
  93. int overwrite = 0;
  94. size_t length = iov_length(iov, nr_segs);
  95. ssize_t ret;
  96. BUG_ON(iocb->ki_pos != pos);
  97. /*
  98. * Unaligned direct AIO must be serialized; see comment above
  99. * In the case of O_APPEND, assume that we must always serialize
  100. */
  101. if (o_direct &&
  102. ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
  103. !is_sync_kiocb(iocb) &&
  104. (file->f_flags & O_APPEND ||
  105. ext4_unaligned_aio(inode, iov, nr_segs, pos))) {
  106. aio_mutex = ext4_aio_mutex(inode);
  107. mutex_lock(aio_mutex);
  108. ext4_unwritten_wait(inode);
  109. }
  110. mutex_lock(&inode->i_mutex);
  111. if (file->f_flags & O_APPEND)
  112. iocb->ki_pos = pos = i_size_read(inode);
  113. /*
  114. * If we have encountered a bitmap-format file, the size limit
  115. * is smaller than s_maxbytes, which is for extent-mapped files.
  116. */
  117. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  118. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  119. if ((pos > sbi->s_bitmap_maxbytes) ||
  120. (pos == sbi->s_bitmap_maxbytes && length > 0)) {
  121. mutex_unlock(&inode->i_mutex);
  122. ret = -EFBIG;
  123. goto errout;
  124. }
  125. if (pos + length > sbi->s_bitmap_maxbytes) {
  126. nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
  127. sbi->s_bitmap_maxbytes - pos);
  128. }
  129. }
  130. if (o_direct) {
  131. blk_start_plug(&plug);
  132. iocb->private = &overwrite;
  133. /* check whether we do a DIO overwrite or not */
  134. if (ext4_should_dioread_nolock(inode) && !aio_mutex &&
  135. !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) {
  136. struct ext4_map_blocks map;
  137. unsigned int blkbits = inode->i_blkbits;
  138. int err, len;
  139. map.m_lblk = pos >> blkbits;
  140. map.m_len = (EXT4_BLOCK_ALIGN(pos + length, blkbits) >> blkbits)
  141. - map.m_lblk;
  142. len = map.m_len;
  143. err = ext4_map_blocks(NULL, inode, &map, 0);
  144. /*
  145. * 'err==len' means that all of blocks has
  146. * been preallocated no matter they are
  147. * initialized or not. For excluding
  148. * unwritten extents, we need to check
  149. * m_flags. There are two conditions that
  150. * indicate for initialized extents. 1) If we
  151. * hit extent cache, EXT4_MAP_MAPPED flag is
  152. * returned; 2) If we do a real lookup,
  153. * non-flags are returned. So we should check
  154. * these two conditions.
  155. */
  156. if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
  157. overwrite = 1;
  158. }
  159. }
  160. ret = __generic_file_aio_write(iocb, iov, nr_segs);
  161. mutex_unlock(&inode->i_mutex);
  162. if (ret > 0) {
  163. ssize_t err;
  164. err = generic_write_sync(file, iocb->ki_pos - ret, ret);
  165. if (err < 0)
  166. ret = err;
  167. }
  168. if (o_direct)
  169. blk_finish_plug(&plug);
  170. errout:
  171. if (aio_mutex)
  172. mutex_unlock(aio_mutex);
  173. return ret;
  174. }
  175. static const struct vm_operations_struct ext4_file_vm_ops = {
  176. .fault = filemap_fault,
  177. .map_pages = filemap_map_pages,
  178. .page_mkwrite = ext4_page_mkwrite,
  179. .remap_pages = generic_file_remap_pages,
  180. };
  181. static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
  182. {
  183. struct address_space *mapping = file->f_mapping;
  184. if (!mapping->a_ops->readpage)
  185. return -ENOEXEC;
  186. file_accessed(file);
  187. vma->vm_ops = &ext4_file_vm_ops;
  188. return 0;
  189. }
  190. static int ext4_file_open(struct inode * inode, struct file * filp)
  191. {
  192. struct super_block *sb = inode->i_sb;
  193. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  194. struct vfsmount *mnt = filp->f_path.mnt;
  195. struct path path;
  196. char buf[64], *cp;
  197. if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
  198. !(sb->s_flags & MS_RDONLY))) {
  199. sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
  200. /*
  201. * Sample where the filesystem has been mounted and
  202. * store it in the superblock for sysadmin convenience
  203. * when trying to sort through large numbers of block
  204. * devices or filesystem images.
  205. */
  206. memset(buf, 0, sizeof(buf));
  207. path.mnt = mnt;
  208. path.dentry = mnt->mnt_root;
  209. cp = d_path(&path, buf, sizeof(buf));
  210. if (!IS_ERR(cp)) {
  211. handle_t *handle;
  212. int err;
  213. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  214. if (IS_ERR(handle))
  215. return PTR_ERR(handle);
  216. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  217. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  218. if (err) {
  219. ext4_journal_stop(handle);
  220. return err;
  221. }
  222. strlcpy(sbi->s_es->s_last_mounted, cp,
  223. sizeof(sbi->s_es->s_last_mounted));
  224. ext4_handle_dirty_super(handle, sb);
  225. ext4_journal_stop(handle);
  226. }
  227. }
  228. /*
  229. * Set up the jbd2_inode if we are opening the inode for
  230. * writing and the journal is present
  231. */
  232. if (filp->f_mode & FMODE_WRITE) {
  233. int ret = ext4_inode_attach_jinode(inode);
  234. if (ret < 0)
  235. return ret;
  236. }
  237. return dquot_file_open(inode, filp);
  238. }
  239. /*
  240. * Here we use ext4_map_blocks() to get a block mapping for a extent-based
  241. * file rather than ext4_ext_walk_space() because we can introduce
  242. * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
  243. * function. When extent status tree has been fully implemented, it will
  244. * track all extent status for a file and we can directly use it to
  245. * retrieve the offset for SEEK_DATA/SEEK_HOLE.
  246. */
  247. /*
  248. * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
  249. * lookup page cache to check whether or not there has some data between
  250. * [startoff, endoff] because, if this range contains an unwritten extent,
  251. * we determine this extent as a data or a hole according to whether the
  252. * page cache has data or not.
  253. */
  254. static int ext4_find_unwritten_pgoff(struct inode *inode,
  255. int whence,
  256. struct ext4_map_blocks *map,
  257. loff_t *offset)
  258. {
  259. struct pagevec pvec;
  260. unsigned int blkbits;
  261. pgoff_t index;
  262. pgoff_t end;
  263. loff_t endoff;
  264. loff_t startoff;
  265. loff_t lastoff;
  266. int found = 0;
  267. blkbits = inode->i_sb->s_blocksize_bits;
  268. startoff = *offset;
  269. lastoff = startoff;
  270. endoff = (loff_t)(map->m_lblk + map->m_len) << blkbits;
  271. index = startoff >> PAGE_CACHE_SHIFT;
  272. end = endoff >> PAGE_CACHE_SHIFT;
  273. pagevec_init(&pvec, 0);
  274. do {
  275. int i, num;
  276. unsigned long nr_pages;
  277. num = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
  278. nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
  279. (pgoff_t)num);
  280. if (nr_pages == 0) {
  281. if (whence == SEEK_DATA)
  282. break;
  283. BUG_ON(whence != SEEK_HOLE);
  284. /*
  285. * If this is the first time to go into the loop and
  286. * offset is not beyond the end offset, it will be a
  287. * hole at this offset
  288. */
  289. if (lastoff == startoff || lastoff < endoff)
  290. found = 1;
  291. break;
  292. }
  293. /*
  294. * If this is the first time to go into the loop and
  295. * offset is smaller than the first page offset, it will be a
  296. * hole at this offset.
  297. */
  298. if (lastoff == startoff && whence == SEEK_HOLE &&
  299. lastoff < page_offset(pvec.pages[0])) {
  300. found = 1;
  301. break;
  302. }
  303. for (i = 0; i < nr_pages; i++) {
  304. struct page *page = pvec.pages[i];
  305. struct buffer_head *bh, *head;
  306. /*
  307. * If the current offset is not beyond the end of given
  308. * range, it will be a hole.
  309. */
  310. if (lastoff < endoff && whence == SEEK_HOLE &&
  311. page->index > end) {
  312. found = 1;
  313. *offset = lastoff;
  314. goto out;
  315. }
  316. lock_page(page);
  317. if (unlikely(page->mapping != inode->i_mapping)) {
  318. unlock_page(page);
  319. continue;
  320. }
  321. if (!page_has_buffers(page)) {
  322. unlock_page(page);
  323. continue;
  324. }
  325. if (page_has_buffers(page)) {
  326. lastoff = page_offset(page);
  327. bh = head = page_buffers(page);
  328. do {
  329. if (buffer_uptodate(bh) ||
  330. buffer_unwritten(bh)) {
  331. if (whence == SEEK_DATA)
  332. found = 1;
  333. } else {
  334. if (whence == SEEK_HOLE)
  335. found = 1;
  336. }
  337. if (found) {
  338. *offset = max_t(loff_t,
  339. startoff, lastoff);
  340. unlock_page(page);
  341. goto out;
  342. }
  343. lastoff += bh->b_size;
  344. bh = bh->b_this_page;
  345. } while (bh != head);
  346. }
  347. lastoff = page_offset(page) + PAGE_SIZE;
  348. unlock_page(page);
  349. }
  350. /*
  351. * The no. of pages is less than our desired, that would be a
  352. * hole in there.
  353. */
  354. if (nr_pages < num && whence == SEEK_HOLE) {
  355. found = 1;
  356. *offset = lastoff;
  357. break;
  358. }
  359. index = pvec.pages[i - 1]->index + 1;
  360. pagevec_release(&pvec);
  361. } while (index <= end);
  362. out:
  363. pagevec_release(&pvec);
  364. return found;
  365. }
  366. /*
  367. * ext4_seek_data() retrieves the offset for SEEK_DATA.
  368. */
  369. static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
  370. {
  371. struct inode *inode = file->f_mapping->host;
  372. struct ext4_map_blocks map;
  373. struct extent_status es;
  374. ext4_lblk_t start, last, end;
  375. loff_t dataoff, isize;
  376. int blkbits;
  377. int ret = 0;
  378. mutex_lock(&inode->i_mutex);
  379. isize = i_size_read(inode);
  380. if (offset >= isize) {
  381. mutex_unlock(&inode->i_mutex);
  382. return -ENXIO;
  383. }
  384. blkbits = inode->i_sb->s_blocksize_bits;
  385. start = offset >> blkbits;
  386. last = start;
  387. end = isize >> blkbits;
  388. dataoff = offset;
  389. do {
  390. map.m_lblk = last;
  391. map.m_len = end - last + 1;
  392. ret = ext4_map_blocks(NULL, inode, &map, 0);
  393. if (ret > 0 && !(map.m_flags & EXT4_MAP_UNWRITTEN)) {
  394. if (last != start)
  395. dataoff = (loff_t)last << blkbits;
  396. break;
  397. }
  398. /*
  399. * If there is a delay extent at this offset,
  400. * it will be as a data.
  401. */
  402. ext4_es_find_delayed_extent_range(inode, last, last, &es);
  403. if (es.es_len != 0 && in_range(last, es.es_lblk, es.es_len)) {
  404. if (last != start)
  405. dataoff = (loff_t)last << blkbits;
  406. break;
  407. }
  408. /*
  409. * If there is a unwritten extent at this offset,
  410. * it will be as a data or a hole according to page
  411. * cache that has data or not.
  412. */
  413. if (map.m_flags & EXT4_MAP_UNWRITTEN) {
  414. int unwritten;
  415. unwritten = ext4_find_unwritten_pgoff(inode, SEEK_DATA,
  416. &map, &dataoff);
  417. if (unwritten)
  418. break;
  419. }
  420. last++;
  421. dataoff = (loff_t)last << blkbits;
  422. } while (last <= end);
  423. mutex_unlock(&inode->i_mutex);
  424. if (dataoff > isize)
  425. return -ENXIO;
  426. return vfs_setpos(file, dataoff, maxsize);
  427. }
  428. /*
  429. * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
  430. */
  431. static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
  432. {
  433. struct inode *inode = file->f_mapping->host;
  434. struct ext4_map_blocks map;
  435. struct extent_status es;
  436. ext4_lblk_t start, last, end;
  437. loff_t holeoff, isize;
  438. int blkbits;
  439. int ret = 0;
  440. mutex_lock(&inode->i_mutex);
  441. isize = i_size_read(inode);
  442. if (offset >= isize) {
  443. mutex_unlock(&inode->i_mutex);
  444. return -ENXIO;
  445. }
  446. blkbits = inode->i_sb->s_blocksize_bits;
  447. start = offset >> blkbits;
  448. last = start;
  449. end = isize >> blkbits;
  450. holeoff = offset;
  451. do {
  452. map.m_lblk = last;
  453. map.m_len = end - last + 1;
  454. ret = ext4_map_blocks(NULL, inode, &map, 0);
  455. if (ret > 0 && !(map.m_flags & EXT4_MAP_UNWRITTEN)) {
  456. last += ret;
  457. holeoff = (loff_t)last << blkbits;
  458. continue;
  459. }
  460. /*
  461. * If there is a delay extent at this offset,
  462. * we will skip this extent.
  463. */
  464. ext4_es_find_delayed_extent_range(inode, last, last, &es);
  465. if (es.es_len != 0 && in_range(last, es.es_lblk, es.es_len)) {
  466. last = es.es_lblk + es.es_len;
  467. holeoff = (loff_t)last << blkbits;
  468. continue;
  469. }
  470. /*
  471. * If there is a unwritten extent at this offset,
  472. * it will be as a data or a hole according to page
  473. * cache that has data or not.
  474. */
  475. if (map.m_flags & EXT4_MAP_UNWRITTEN) {
  476. int unwritten;
  477. unwritten = ext4_find_unwritten_pgoff(inode, SEEK_HOLE,
  478. &map, &holeoff);
  479. if (!unwritten) {
  480. last += ret;
  481. holeoff = (loff_t)last << blkbits;
  482. continue;
  483. }
  484. }
  485. /* find a hole */
  486. break;
  487. } while (last <= end);
  488. mutex_unlock(&inode->i_mutex);
  489. if (holeoff > isize)
  490. holeoff = isize;
  491. return vfs_setpos(file, holeoff, maxsize);
  492. }
  493. /*
  494. * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
  495. * by calling generic_file_llseek_size() with the appropriate maxbytes
  496. * value for each.
  497. */
  498. loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
  499. {
  500. struct inode *inode = file->f_mapping->host;
  501. loff_t maxbytes;
  502. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  503. maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  504. else
  505. maxbytes = inode->i_sb->s_maxbytes;
  506. switch (whence) {
  507. case SEEK_SET:
  508. case SEEK_CUR:
  509. case SEEK_END:
  510. return generic_file_llseek_size(file, offset, whence,
  511. maxbytes, i_size_read(inode));
  512. case SEEK_DATA:
  513. return ext4_seek_data(file, offset, maxbytes);
  514. case SEEK_HOLE:
  515. return ext4_seek_hole(file, offset, maxbytes);
  516. }
  517. return -EINVAL;
  518. }
  519. const struct file_operations ext4_file_operations = {
  520. .llseek = ext4_llseek,
  521. .read = do_sync_read,
  522. .write = do_sync_write,
  523. .aio_read = generic_file_aio_read,
  524. .aio_write = ext4_file_write,
  525. .unlocked_ioctl = ext4_ioctl,
  526. #ifdef CONFIG_COMPAT
  527. .compat_ioctl = ext4_compat_ioctl,
  528. #endif
  529. .mmap = ext4_file_mmap,
  530. .open = ext4_file_open,
  531. .release = ext4_release_file,
  532. .fsync = ext4_sync_file,
  533. .splice_read = generic_file_splice_read,
  534. .splice_write = generic_file_splice_write,
  535. .fallocate = ext4_fallocate,
  536. };
  537. const struct inode_operations ext4_file_inode_operations = {
  538. .setattr = ext4_setattr,
  539. .getattr = ext4_getattr,
  540. .setxattr = generic_setxattr,
  541. .getxattr = generic_getxattr,
  542. .listxattr = ext4_listxattr,
  543. .removexattr = generic_removexattr,
  544. .get_acl = ext4_get_acl,
  545. .set_acl = ext4_set_acl,
  546. .fiemap = ext4_fiemap,
  547. };