| .. | .. |
|---|
| 22 | 22 | SYNC_FILE_RANGE_WAIT_AFTER) |
|---|
| 23 | 23 | |
|---|
| 24 | 24 | /* |
|---|
| 25 | | - * Do the filesystem syncing work. For simple filesystems |
|---|
| 26 | | - * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to |
|---|
| 27 | | - * submit IO for these buffers via __sync_blockdev(). This also speeds up the |
|---|
| 28 | | - * wait == 1 case since in that case write_inode() functions do |
|---|
| 29 | | - * sync_dirty_buffer() and thus effectively write one block at a time. |
|---|
| 30 | | - */ |
|---|
| 31 | | -static int __sync_filesystem(struct super_block *sb, int wait) |
|---|
| 32 | | -{ |
|---|
| 33 | | - if (wait) |
|---|
| 34 | | - sync_inodes_sb(sb); |
|---|
| 35 | | - else |
|---|
| 36 | | - writeback_inodes_sb(sb, WB_REASON_SYNC); |
|---|
| 37 | | - |
|---|
| 38 | | - if (sb->s_op->sync_fs) |
|---|
| 39 | | - sb->s_op->sync_fs(sb, wait); |
|---|
| 40 | | - return __sync_blockdev(sb->s_bdev, wait); |
|---|
| 41 | | -} |
|---|
| 42 | | - |
|---|
| 43 | | -/* |
|---|
| 44 | 25 | * Write out and wait upon all dirty data associated with this |
|---|
| 45 | 26 | * superblock. Filesystem data as well as the underlying block |
|---|
| 46 | 27 | * device. Takes the superblock lock. |
|---|
| 47 | 28 | */ |
|---|
| 48 | 29 | int sync_filesystem(struct super_block *sb) |
|---|
| 49 | 30 | { |
|---|
| 50 | | - int ret; |
|---|
| 31 | + int ret = 0; |
|---|
| 51 | 32 | |
|---|
| 52 | 33 | /* |
|---|
| 53 | 34 | * We need to be protected against the filesystem going from |
|---|
| .. | .. |
|---|
| 61 | 42 | if (sb_rdonly(sb)) |
|---|
| 62 | 43 | return 0; |
|---|
| 63 | 44 | |
|---|
| 64 | | - ret = __sync_filesystem(sb, 0); |
|---|
| 65 | | - if (ret < 0) |
|---|
| 45 | + /* |
|---|
| 46 | + * Do the filesystem syncing work. For simple filesystems |
|---|
| 47 | + * writeback_inodes_sb(sb) just dirties buffers with inodes so we have |
|---|
| 48 | + * to submit I/O for these buffers via __sync_blockdev(). This also |
|---|
| 49 | + * speeds up the wait == 1 case since in that case write_inode() |
|---|
| 50 | + * methods call sync_dirty_buffer() and thus effectively write one block |
|---|
| 51 | + * at a time. |
|---|
| 52 | + */ |
|---|
| 53 | + writeback_inodes_sb(sb, WB_REASON_SYNC); |
|---|
| 54 | + if (sb->s_op->sync_fs) { |
|---|
| 55 | + ret = sb->s_op->sync_fs(sb, 0); |
|---|
| 56 | + if (ret) |
|---|
| 57 | + return ret; |
|---|
| 58 | + } |
|---|
| 59 | + ret = __sync_blockdev(sb->s_bdev, 0); |
|---|
| 60 | + if (ret) |
|---|
| 66 | 61 | return ret; |
|---|
| 67 | | - return __sync_filesystem(sb, 1); |
|---|
| 62 | + |
|---|
| 63 | + sync_inodes_sb(sb); |
|---|
| 64 | + if (sb->s_op->sync_fs) { |
|---|
| 65 | + ret = sb->s_op->sync_fs(sb, 1); |
|---|
| 66 | + if (ret) |
|---|
| 67 | + return ret; |
|---|
| 68 | + } |
|---|
| 69 | + return __sync_blockdev(sb->s_bdev, 1); |
|---|
| 68 | 70 | } |
|---|
| 69 | | -EXPORT_SYMBOL(sync_filesystem); |
|---|
| 71 | +EXPORT_SYMBOL_NS(sync_filesystem, ANDROID_GKI_VFS_EXPORT_ONLY); |
|---|
| 70 | 72 | |
|---|
| 71 | 73 | static void sync_inodes_one_sb(struct super_block *sb, void *arg) |
|---|
| 72 | 74 | { |
|---|
| .. | .. |
|---|
| 76 | 78 | |
|---|
| 77 | 79 | static void sync_fs_one_sb(struct super_block *sb, void *arg) |
|---|
| 78 | 80 | { |
|---|
| 79 | | - if (!sb_rdonly(sb) && sb->s_op->sync_fs) |
|---|
| 81 | + if (!sb_rdonly(sb) && !(sb->s_iflags & SB_I_SKIP_SYNC) && |
|---|
| 82 | + sb->s_op->sync_fs) |
|---|
| 80 | 83 | sb->s_op->sync_fs(sb, *(int *)arg); |
|---|
| 81 | 84 | } |
|---|
| 82 | 85 | |
|---|
| .. | .. |
|---|
| 161 | 164 | { |
|---|
| 162 | 165 | struct fd f = fdget(fd); |
|---|
| 163 | 166 | struct super_block *sb; |
|---|
| 164 | | - int ret; |
|---|
| 167 | + int ret, ret2; |
|---|
| 165 | 168 | |
|---|
| 166 | 169 | if (!f.file) |
|---|
| 167 | 170 | return -EBADF; |
|---|
| .. | .. |
|---|
| 171 | 174 | ret = sync_filesystem(sb); |
|---|
| 172 | 175 | up_read(&sb->s_umount); |
|---|
| 173 | 176 | |
|---|
| 177 | + ret2 = errseq_check_and_advance(&sb->s_wb_err, &f.file->f_sb_err); |
|---|
| 178 | + |
|---|
| 174 | 179 | fdput(f); |
|---|
| 175 | | - return ret; |
|---|
| 180 | + return ret ? ret : ret2; |
|---|
| 176 | 181 | } |
|---|
| 177 | 182 | |
|---|
| 178 | 183 | /** |
|---|
| .. | .. |
|---|
| 235 | 240 | return do_fsync(fd, 1); |
|---|
| 236 | 241 | } |
|---|
| 237 | 242 | |
|---|
| 238 | | -/* |
|---|
| 239 | | - * sys_sync_file_range() permits finely controlled syncing over a segment of |
|---|
| 240 | | - * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is |
|---|
| 241 | | - * zero then sys_sync_file_range() will operate from offset out to EOF. |
|---|
| 242 | | - * |
|---|
| 243 | | - * The flag bits are: |
|---|
| 244 | | - * |
|---|
| 245 | | - * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range |
|---|
| 246 | | - * before performing the write. |
|---|
| 247 | | - * |
|---|
| 248 | | - * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the |
|---|
| 249 | | - * range which are not presently under writeback. Note that this may block for |
|---|
| 250 | | - * significant periods due to exhaustion of disk request structures. |
|---|
| 251 | | - * |
|---|
| 252 | | - * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range |
|---|
| 253 | | - * after performing the write. |
|---|
| 254 | | - * |
|---|
| 255 | | - * Useful combinations of the flag bits are: |
|---|
| 256 | | - * |
|---|
| 257 | | - * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages |
|---|
| 258 | | - * in the range which were dirty on entry to sys_sync_file_range() are placed |
|---|
| 259 | | - * under writeout. This is a start-write-for-data-integrity operation. |
|---|
| 260 | | - * |
|---|
| 261 | | - * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which |
|---|
| 262 | | - * are not presently under writeout. This is an asynchronous flush-to-disk |
|---|
| 263 | | - * operation. Not suitable for data integrity operations. |
|---|
| 264 | | - * |
|---|
| 265 | | - * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for |
|---|
| 266 | | - * completion of writeout of all pages in the range. This will be used after an |
|---|
| 267 | | - * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait |
|---|
| 268 | | - * for that operation to complete and to return the result. |
|---|
| 269 | | - * |
|---|
| 270 | | - * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER: |
|---|
| 271 | | - * a traditional sync() operation. This is a write-for-data-integrity operation |
|---|
| 272 | | - * which will ensure that all pages in the range which were dirty on entry to |
|---|
| 273 | | - * sys_sync_file_range() are committed to disk. |
|---|
| 274 | | - * |
|---|
| 275 | | - * |
|---|
| 276 | | - * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any |
|---|
| 277 | | - * I/O errors or ENOSPC conditions and will return those to the caller, after |
|---|
| 278 | | - * clearing the EIO and ENOSPC flags in the address_space. |
|---|
| 279 | | - * |
|---|
| 280 | | - * It should be noted that none of these operations write out the file's |
|---|
| 281 | | - * metadata. So unless the application is strictly performing overwrites of |
|---|
| 282 | | - * already-instantiated disk blocks, there are no guarantees here that the data |
|---|
| 283 | | - * will be available after a crash. |
|---|
| 284 | | - */ |
|---|
| 285 | | -int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes, |
|---|
| 286 | | - unsigned int flags) |
|---|
| 243 | +int sync_file_range(struct file *file, loff_t offset, loff_t nbytes, |
|---|
| 244 | + unsigned int flags) |
|---|
| 287 | 245 | { |
|---|
| 288 | 246 | int ret; |
|---|
| 289 | | - struct fd f; |
|---|
| 290 | 247 | struct address_space *mapping; |
|---|
| 291 | 248 | loff_t endbyte; /* inclusive */ |
|---|
| 292 | 249 | umode_t i_mode; |
|---|
| .. | .. |
|---|
| 326 | 283 | else |
|---|
| 327 | 284 | endbyte--; /* inclusive */ |
|---|
| 328 | 285 | |
|---|
| 329 | | - ret = -EBADF; |
|---|
| 330 | | - f = fdget(fd); |
|---|
| 331 | | - if (!f.file) |
|---|
| 332 | | - goto out; |
|---|
| 333 | | - |
|---|
| 334 | | - i_mode = file_inode(f.file)->i_mode; |
|---|
| 286 | + i_mode = file_inode(file)->i_mode; |
|---|
| 335 | 287 | ret = -ESPIPE; |
|---|
| 336 | 288 | if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) && |
|---|
| 337 | 289 | !S_ISLNK(i_mode)) |
|---|
| 338 | | - goto out_put; |
|---|
| 290 | + goto out; |
|---|
| 339 | 291 | |
|---|
| 340 | | - mapping = f.file->f_mapping; |
|---|
| 292 | + mapping = file->f_mapping; |
|---|
| 341 | 293 | ret = 0; |
|---|
| 342 | 294 | if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) { |
|---|
| 343 | | - ret = file_fdatawait_range(f.file, offset, endbyte); |
|---|
| 295 | + ret = file_fdatawait_range(file, offset, endbyte); |
|---|
| 344 | 296 | if (ret < 0) |
|---|
| 345 | | - goto out_put; |
|---|
| 297 | + goto out; |
|---|
| 346 | 298 | } |
|---|
| 347 | 299 | |
|---|
| 348 | 300 | if (flags & SYNC_FILE_RANGE_WRITE) { |
|---|
| 301 | + int sync_mode = WB_SYNC_NONE; |
|---|
| 302 | + |
|---|
| 303 | + if ((flags & SYNC_FILE_RANGE_WRITE_AND_WAIT) == |
|---|
| 304 | + SYNC_FILE_RANGE_WRITE_AND_WAIT) |
|---|
| 305 | + sync_mode = WB_SYNC_ALL; |
|---|
| 306 | + |
|---|
| 349 | 307 | ret = __filemap_fdatawrite_range(mapping, offset, endbyte, |
|---|
| 350 | | - WB_SYNC_NONE); |
|---|
| 308 | + sync_mode); |
|---|
| 351 | 309 | if (ret < 0) |
|---|
| 352 | | - goto out_put; |
|---|
| 310 | + goto out; |
|---|
| 353 | 311 | } |
|---|
| 354 | 312 | |
|---|
| 355 | 313 | if (flags & SYNC_FILE_RANGE_WAIT_AFTER) |
|---|
| 356 | | - ret = file_fdatawait_range(f.file, offset, endbyte); |
|---|
| 314 | + ret = file_fdatawait_range(file, offset, endbyte); |
|---|
| 357 | 315 | |
|---|
| 358 | | -out_put: |
|---|
| 359 | | - fdput(f); |
|---|
| 360 | 316 | out: |
|---|
| 361 | 317 | return ret; |
|---|
| 362 | 318 | } |
|---|
| 363 | 319 | |
|---|
| 320 | +/* |
|---|
| 321 | + * ksys_sync_file_range() permits finely controlled syncing over a segment of |
|---|
| 322 | + * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is |
|---|
| 323 | + * zero then ksys_sync_file_range() will operate from offset out to EOF. |
|---|
| 324 | + * |
|---|
| 325 | + * The flag bits are: |
|---|
| 326 | + * |
|---|
| 327 | + * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range |
|---|
| 328 | + * before performing the write. |
|---|
| 329 | + * |
|---|
| 330 | + * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the |
|---|
| 331 | + * range which are not presently under writeback. Note that this may block for |
|---|
| 332 | + * significant periods due to exhaustion of disk request structures. |
|---|
| 333 | + * |
|---|
| 334 | + * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range |
|---|
| 335 | + * after performing the write. |
|---|
| 336 | + * |
|---|
| 337 | + * Useful combinations of the flag bits are: |
|---|
| 338 | + * |
|---|
| 339 | + * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages |
|---|
| 340 | + * in the range which were dirty on entry to ksys_sync_file_range() are placed |
|---|
| 341 | + * under writeout. This is a start-write-for-data-integrity operation. |
|---|
| 342 | + * |
|---|
| 343 | + * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which |
|---|
| 344 | + * are not presently under writeout. This is an asynchronous flush-to-disk |
|---|
| 345 | + * operation. Not suitable for data integrity operations. |
|---|
| 346 | + * |
|---|
| 347 | + * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for |
|---|
| 348 | + * completion of writeout of all pages in the range. This will be used after an |
|---|
| 349 | + * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait |
|---|
| 350 | + * for that operation to complete and to return the result. |
|---|
| 351 | + * |
|---|
| 352 | + * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER |
|---|
| 353 | + * (a.k.a. SYNC_FILE_RANGE_WRITE_AND_WAIT): |
|---|
| 354 | + * a traditional sync() operation. This is a write-for-data-integrity operation |
|---|
| 355 | + * which will ensure that all pages in the range which were dirty on entry to |
|---|
| 356 | + * ksys_sync_file_range() are written to disk. It should be noted that disk |
|---|
| 357 | + * caches are not flushed by this call, so there are no guarantees here that the |
|---|
| 358 | + * data will be available on disk after a crash. |
|---|
| 359 | + * |
|---|
| 360 | + * |
|---|
| 361 | + * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any |
|---|
| 362 | + * I/O errors or ENOSPC conditions and will return those to the caller, after |
|---|
| 363 | + * clearing the EIO and ENOSPC flags in the address_space. |
|---|
| 364 | + * |
|---|
| 365 | + * It should be noted that none of these operations write out the file's |
|---|
| 366 | + * metadata. So unless the application is strictly performing overwrites of |
|---|
| 367 | + * already-instantiated disk blocks, there are no guarantees here that the data |
|---|
| 368 | + * will be available after a crash. |
|---|
| 369 | + */ |
|---|
| 370 | +int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes, |
|---|
| 371 | + unsigned int flags) |
|---|
| 372 | +{ |
|---|
| 373 | + int ret; |
|---|
| 374 | + struct fd f; |
|---|
| 375 | + |
|---|
| 376 | + ret = -EBADF; |
|---|
| 377 | + f = fdget(fd); |
|---|
| 378 | + if (f.file) |
|---|
| 379 | + ret = sync_file_range(f.file, offset, nbytes, flags); |
|---|
| 380 | + |
|---|
| 381 | + fdput(f); |
|---|
| 382 | + return ret; |
|---|
| 383 | +} |
|---|
| 384 | + |
|---|
| 364 | 385 | SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes, |
|---|
| 365 | 386 | unsigned int, flags) |
|---|
| 366 | 387 | { |
|---|