file.c 16 KB

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