4 * Copyright (C) 1992, 1993, 1994, 1995
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
11 * linux/fs/minix/file.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * ext4 fs regular file handling primitives
17 * 64-bit file support on 64-bit platforms by Jakub Jelinek
21 #include <linux/time.h>
23 #include <linux/jbd2.h>
24 #include <linux/mount.h>
25 #include <linux/path.h>
26 #include <linux/quotaops.h>
27 #include <linux/pagevec.h>
29 #include "ext4_jbd2.h"
34 * Called when an inode is released. Note that this is different
35 * from ext4_file_open: open gets called at every open, but release
36 * gets called only when /all/ the files are closed.
38 static int ext4_release_file(struct inode *inode, struct file *filp)
40 if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
41 ext4_alloc_da_blocks(inode);
42 ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
44 /* if we are the last writer on the inode, drop the block reservation */
45 if ((filp->f_mode & FMODE_WRITE) &&
46 (atomic_read(&inode->i_writecount) == 1) &&
47 !EXT4_I(inode)->i_reserved_data_blocks)
49 down_write(&EXT4_I(inode)->i_data_sem);
50 ext4_discard_preallocations(inode);
51 up_write(&EXT4_I(inode)->i_data_sem);
53 if (is_dx(inode) && filp->private_data)
54 ext4_htree_free_dir_info(filp->private_data);
59 void ext4_unwritten_wait(struct inode *inode)
61 wait_queue_head_t *wq = ext4_ioend_wq(inode);
63 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_unwritten) == 0));
67 * This tests whether the IO in question is block-aligned or not.
68 * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
69 * are converted to written only after the IO is complete. Until they are
70 * mapped, these blocks appear as holes, so dio_zero_block() will assume that
71 * it needs to zero out portions of the start and/or end block. If 2 AIO
72 * threads are at work on the same unwritten block, they must be synchronized
73 * or one thread will zero the other's data, causing corruption.
76 ext4_unaligned_aio(struct inode *inode, const struct iovec *iov,
77 unsigned long nr_segs, loff_t pos)
79 struct super_block *sb = inode->i_sb;
80 int blockmask = sb->s_blocksize - 1;
81 size_t count = iov_length(iov, nr_segs);
82 loff_t final_size = pos + count;
84 if (pos >= inode->i_size)
87 if ((pos & blockmask) || (final_size & blockmask))
94 ext4_file_dio_write(struct kiocb *iocb, const struct iovec *iov,
95 unsigned long nr_segs, loff_t pos)
97 struct file *file = iocb->ki_filp;
98 struct inode *inode = file->f_mapping->host;
100 int unaligned_aio = 0;
103 size_t length = iov_length(iov, nr_segs);
105 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
106 !is_sync_kiocb(iocb))
107 unaligned_aio = ext4_unaligned_aio(inode, iov, nr_segs, pos);
109 /* Unaligned direct AIO must be serialized; see comment above */
111 mutex_lock(ext4_aio_mutex(inode));
112 ext4_unwritten_wait(inode);
115 BUG_ON(iocb->ki_pos != pos);
117 mutex_lock(&inode->i_mutex);
118 blk_start_plug(&plug);
120 iocb->private = &overwrite;
122 /* check whether we do a DIO overwrite or not */
123 if (ext4_should_dioread_nolock(inode) && !unaligned_aio &&
124 !file->f_mapping->nrpages && pos + length <= i_size_read(inode)) {
125 struct ext4_map_blocks map;
126 unsigned int blkbits = inode->i_blkbits;
129 map.m_lblk = pos >> blkbits;
130 map.m_len = (EXT4_BLOCK_ALIGN(pos + length, blkbits) >> blkbits)
134 err = ext4_map_blocks(NULL, inode, &map, 0);
136 * 'err==len' means that all of blocks has been preallocated no
137 * matter they are initialized or not. For excluding
138 * uninitialized extents, we need to check m_flags. There are
139 * two conditions that indicate for initialized extents.
140 * 1) If we hit extent cache, EXT4_MAP_MAPPED flag is returned;
141 * 2) If we do a real lookup, non-flags are returned.
142 * So we should check these two conditions.
144 if (err == len && (map.m_flags & EXT4_MAP_MAPPED))
148 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
149 mutex_unlock(&inode->i_mutex);
151 if (ret > 0 || ret == -EIOCBQUEUED) {
154 err = generic_write_sync(file, pos, ret);
155 if (err < 0 && ret > 0)
158 blk_finish_plug(&plug);
161 mutex_unlock(ext4_aio_mutex(inode));
167 ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
168 unsigned long nr_segs, loff_t pos)
170 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
174 * If we have encountered a bitmap-format file, the size limit
175 * is smaller than s_maxbytes, which is for extent-mapped files.
178 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
179 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
180 size_t length = iov_length(iov, nr_segs);
182 if ((pos > sbi->s_bitmap_maxbytes ||
183 (pos == sbi->s_bitmap_maxbytes && length > 0)))
186 if (pos + length > sbi->s_bitmap_maxbytes) {
187 nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
188 sbi->s_bitmap_maxbytes - pos);
192 if (unlikely(iocb->ki_filp->f_flags & O_DIRECT))
193 ret = ext4_file_dio_write(iocb, iov, nr_segs, pos);
195 ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
200 static const struct vm_operations_struct ext4_file_vm_ops = {
201 .fault = filemap_fault,
202 .page_mkwrite = ext4_page_mkwrite,
203 .remap_pages = generic_file_remap_pages,
206 static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
208 struct address_space *mapping = file->f_mapping;
210 if (!mapping->a_ops->readpage)
213 vma->vm_ops = &ext4_file_vm_ops;
217 static int ext4_file_open(struct inode * inode, struct file * filp)
219 struct super_block *sb = inode->i_sb;
220 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
221 struct ext4_inode_info *ei = EXT4_I(inode);
222 struct vfsmount *mnt = filp->f_path.mnt;
226 if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
227 !(sb->s_flags & MS_RDONLY))) {
228 sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
230 * Sample where the filesystem has been mounted and
231 * store it in the superblock for sysadmin convenience
232 * when trying to sort through large numbers of block
233 * devices or filesystem images.
235 memset(buf, 0, sizeof(buf));
237 path.dentry = mnt->mnt_root;
238 cp = d_path(&path, buf, sizeof(buf));
243 handle = ext4_journal_start_sb(sb, 1);
245 return PTR_ERR(handle);
246 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
248 ext4_journal_stop(handle);
251 strlcpy(sbi->s_es->s_last_mounted, cp,
252 sizeof(sbi->s_es->s_last_mounted));
253 ext4_handle_dirty_super(handle, sb);
254 ext4_journal_stop(handle);
258 * Set up the jbd2_inode if we are opening the inode for
259 * writing and the journal is present
261 if (sbi->s_journal && !ei->jinode && (filp->f_mode & FMODE_WRITE)) {
262 struct jbd2_inode *jinode = jbd2_alloc_inode(GFP_KERNEL);
264 spin_lock(&inode->i_lock);
267 spin_unlock(&inode->i_lock);
271 jbd2_journal_init_jbd_inode(ei->jinode, inode);
274 spin_unlock(&inode->i_lock);
275 if (unlikely(jinode != NULL))
276 jbd2_free_inode(jinode);
278 return dquot_file_open(inode, filp);
282 * Here we use ext4_map_blocks() to get a block mapping for a extent-based
283 * file rather than ext4_ext_walk_space() because we can introduce
284 * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
285 * function. When extent status tree has been fully implemented, it will
286 * track all extent status for a file and we can directly use it to
287 * retrieve the offset for SEEK_DATA/SEEK_HOLE.
291 * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
292 * lookup page cache to check whether or not there has some data between
293 * [startoff, endoff] because, if this range contains an unwritten extent,
294 * we determine this extent as a data or a hole according to whether the
295 * page cache has data or not.
297 static int ext4_find_unwritten_pgoff(struct inode *inode,
299 struct ext4_map_blocks *map,
303 unsigned int blkbits;
311 blkbits = inode->i_sb->s_blocksize_bits;
314 endoff = (map->m_lblk + map->m_len) << blkbits;
316 index = startoff >> PAGE_CACHE_SHIFT;
317 end = endoff >> PAGE_CACHE_SHIFT;
319 pagevec_init(&pvec, 0);
322 unsigned long nr_pages;
324 num = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
325 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
328 if (whence == SEEK_DATA)
331 BUG_ON(whence != SEEK_HOLE);
333 * If this is the first time to go into the loop and
334 * offset is not beyond the end offset, it will be a
335 * hole at this offset
337 if (lastoff == startoff || lastoff < endoff)
343 * If this is the first time to go into the loop and
344 * offset is smaller than the first page offset, it will be a
345 * hole at this offset.
347 if (lastoff == startoff && whence == SEEK_HOLE &&
348 lastoff < page_offset(pvec.pages[0])) {
353 for (i = 0; i < nr_pages; i++) {
354 struct page *page = pvec.pages[i];
355 struct buffer_head *bh, *head;
358 * If the current offset is not beyond the end of given
359 * range, it will be a hole.
361 if (lastoff < endoff && whence == SEEK_HOLE &&
370 if (unlikely(page->mapping != inode->i_mapping)) {
375 if (!page_has_buffers(page)) {
380 if (page_has_buffers(page)) {
381 lastoff = page_offset(page);
382 bh = head = page_buffers(page);
384 if (buffer_uptodate(bh) ||
385 buffer_unwritten(bh)) {
386 if (whence == SEEK_DATA)
389 if (whence == SEEK_HOLE)
393 *offset = max_t(loff_t,
398 lastoff += bh->b_size;
399 bh = bh->b_this_page;
400 } while (bh != head);
403 lastoff = page_offset(page) + PAGE_SIZE;
408 * The no. of pages is less than our desired, that would be a
411 if (nr_pages < num && whence == SEEK_HOLE) {
417 index = pvec.pages[i - 1]->index + 1;
418 pagevec_release(&pvec);
419 } while (index <= end);
422 pagevec_release(&pvec);
427 * ext4_seek_data() retrieves the offset for SEEK_DATA.
429 static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
431 struct inode *inode = file->f_mapping->host;
432 struct ext4_map_blocks map;
433 struct extent_status es;
434 ext4_lblk_t start, last, end;
435 loff_t dataoff, isize;
439 mutex_lock(&inode->i_mutex);
441 isize = i_size_read(inode);
442 if (offset >= isize) {
443 mutex_unlock(&inode->i_mutex);
447 blkbits = inode->i_sb->s_blocksize_bits;
448 start = offset >> blkbits;
450 end = isize >> blkbits;
455 map.m_len = end - last + 1;
456 ret = ext4_map_blocks(NULL, inode, &map, 0);
457 if (ret > 0 && !(map.m_flags & EXT4_MAP_UNWRITTEN)) {
459 dataoff = last << blkbits;
464 * If there is a delay extent at this offset,
465 * it will be as a data.
468 (void)ext4_es_find_extent(inode, &es);
469 if (last >= es.start &&
470 last < es.start + es.len) {
472 dataoff = last << blkbits;
477 * If there is a unwritten extent at this offset,
478 * it will be as a data or a hole according to page
479 * cache that has data or not.
481 if (map.m_flags & EXT4_MAP_UNWRITTEN) {
483 unwritten = ext4_find_unwritten_pgoff(inode, SEEK_DATA,
490 dataoff = last << blkbits;
491 } while (last <= end);
493 mutex_unlock(&inode->i_mutex);
498 if (dataoff < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
500 if (dataoff > maxsize)
503 if (dataoff != file->f_pos) {
504 file->f_pos = dataoff;
512 * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
514 static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
516 struct inode *inode = file->f_mapping->host;
517 struct ext4_map_blocks map;
518 struct extent_status es;
519 ext4_lblk_t start, last, end;
520 loff_t holeoff, isize;
524 mutex_lock(&inode->i_mutex);
526 isize = i_size_read(inode);
527 if (offset >= isize) {
528 mutex_unlock(&inode->i_mutex);
532 blkbits = inode->i_sb->s_blocksize_bits;
533 start = offset >> blkbits;
535 end = isize >> blkbits;
540 map.m_len = end - last + 1;
541 ret = ext4_map_blocks(NULL, inode, &map, 0);
542 if (ret > 0 && !(map.m_flags & EXT4_MAP_UNWRITTEN)) {
544 holeoff = last << blkbits;
549 * If there is a delay extent at this offset,
550 * we will skip this extent.
553 (void)ext4_es_find_extent(inode, &es);
554 if (last >= es.start &&
555 last < es.start + es.len) {
556 last = es.start + es.len;
557 holeoff = last << blkbits;
562 * If there is a unwritten extent at this offset,
563 * it will be as a data or a hole according to page
564 * cache that has data or not.
566 if (map.m_flags & EXT4_MAP_UNWRITTEN) {
568 unwritten = ext4_find_unwritten_pgoff(inode, SEEK_HOLE,
572 holeoff = last << blkbits;
579 } while (last <= end);
581 mutex_unlock(&inode->i_mutex);
586 if (holeoff < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET))
588 if (holeoff > maxsize)
591 if (holeoff != file->f_pos) {
592 file->f_pos = holeoff;
600 * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
601 * by calling generic_file_llseek_size() with the appropriate maxbytes
604 loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
606 struct inode *inode = file->f_mapping->host;
609 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
610 maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
612 maxbytes = inode->i_sb->s_maxbytes;
618 return generic_file_llseek_size(file, offset, whence,
619 maxbytes, i_size_read(inode));
621 return ext4_seek_data(file, offset, maxbytes);
623 return ext4_seek_hole(file, offset, maxbytes);
629 const struct file_operations ext4_file_operations = {
630 .llseek = ext4_llseek,
631 .read = do_sync_read,
632 .write = do_sync_write,
633 .aio_read = generic_file_aio_read,
634 .aio_write = ext4_file_write,
635 .unlocked_ioctl = ext4_ioctl,
637 .compat_ioctl = ext4_compat_ioctl,
639 .mmap = ext4_file_mmap,
640 .open = ext4_file_open,
641 .release = ext4_release_file,
642 .fsync = ext4_sync_file,
643 .splice_read = generic_file_splice_read,
644 .splice_write = generic_file_splice_write,
645 .fallocate = ext4_fallocate,
648 const struct inode_operations ext4_file_inode_operations = {
649 .setattr = ext4_setattr,
650 .getattr = ext4_getattr,
651 .setxattr = generic_setxattr,
652 .getxattr = generic_getxattr,
653 .listxattr = ext4_listxattr,
654 .removexattr = generic_removexattr,
655 .get_acl = ext4_get_acl,
656 .fiemap = ext4_fiemap,