file.c 16 KB

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