hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/fs/sync.c
....@@ -22,32 +22,13 @@
2222 SYNC_FILE_RANGE_WAIT_AFTER)
2323
2424 /*
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
-/*
4425 * Write out and wait upon all dirty data associated with this
4526 * superblock. Filesystem data as well as the underlying block
4627 * device. Takes the superblock lock.
4728 */
4829 int sync_filesystem(struct super_block *sb)
4930 {
50
- int ret;
31
+ int ret = 0;
5132
5233 /*
5334 * We need to be protected against the filesystem going from
....@@ -61,12 +42,33 @@
6142 if (sb_rdonly(sb))
6243 return 0;
6344
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)
6661 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);
6870 }
69
-EXPORT_SYMBOL(sync_filesystem);
71
+EXPORT_SYMBOL_NS(sync_filesystem, ANDROID_GKI_VFS_EXPORT_ONLY);
7072
7173 static void sync_inodes_one_sb(struct super_block *sb, void *arg)
7274 {
....@@ -76,7 +78,8 @@
7678
7779 static void sync_fs_one_sb(struct super_block *sb, void *arg)
7880 {
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)
8083 sb->s_op->sync_fs(sb, *(int *)arg);
8184 }
8285
....@@ -161,7 +164,7 @@
161164 {
162165 struct fd f = fdget(fd);
163166 struct super_block *sb;
164
- int ret;
167
+ int ret, ret2;
165168
166169 if (!f.file)
167170 return -EBADF;
....@@ -171,8 +174,10 @@
171174 ret = sync_filesystem(sb);
172175 up_read(&sb->s_umount);
173176
177
+ ret2 = errseq_check_and_advance(&sb->s_wb_err, &f.file->f_sb_err);
178
+
174179 fdput(f);
175
- return ret;
180
+ return ret ? ret : ret2;
176181 }
177182
178183 /**
....@@ -235,58 +240,10 @@
235240 return do_fsync(fd, 1);
236241 }
237242
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)
287245 {
288246 int ret;
289
- struct fd f;
290247 struct address_space *mapping;
291248 loff_t endbyte; /* inclusive */
292249 umode_t i_mode;
....@@ -326,41 +283,105 @@
326283 else
327284 endbyte--; /* inclusive */
328285
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;
335287 ret = -ESPIPE;
336288 if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
337289 !S_ISLNK(i_mode))
338
- goto out_put;
290
+ goto out;
339291
340
- mapping = f.file->f_mapping;
292
+ mapping = file->f_mapping;
341293 ret = 0;
342294 if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
343
- ret = file_fdatawait_range(f.file, offset, endbyte);
295
+ ret = file_fdatawait_range(file, offset, endbyte);
344296 if (ret < 0)
345
- goto out_put;
297
+ goto out;
346298 }
347299
348300 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
+
349307 ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
350
- WB_SYNC_NONE);
308
+ sync_mode);
351309 if (ret < 0)
352
- goto out_put;
310
+ goto out;
353311 }
354312
355313 if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
356
- ret = file_fdatawait_range(f.file, offset, endbyte);
314
+ ret = file_fdatawait_range(file, offset, endbyte);
357315
358
-out_put:
359
- fdput(f);
360316 out:
361317 return ret;
362318 }
363319
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
+
364385 SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes,
365386 unsigned int, flags)
366387 {