hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/fs/inode.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * (C) 1997 Linus Torvalds
34 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation)
....@@ -10,7 +11,7 @@
1011 #include <linux/swap.h>
1112 #include <linux/security.h>
1213 #include <linux/cdev.h>
13
-#include <linux/bootmem.h>
14
+#include <linux/memblock.h>
1415 #include <linux/fscrypt.h>
1516 #include <linux/fsnotify.h>
1617 #include <linux/mount.h>
....@@ -107,7 +108,7 @@
107108 */
108109 #ifdef CONFIG_SYSCTL
109110 int proc_nr_inodes(struct ctl_table *table, int write,
110
- void __user *buffer, size_t *lenp, loff_t *ppos)
111
+ void *buffer, size_t *lenp, loff_t *ppos)
111112 {
112113 inodes_stat.nr_inodes = get_nr_inodes();
113114 inodes_stat.nr_unused = get_nr_inodes_unused();
....@@ -157,7 +158,7 @@
157158 inode->i_bdev = NULL;
158159 inode->i_cdev = NULL;
159160 inode->i_link = NULL;
160
- inode->__i_dir_seq = 0;
161
+ inode->i_dir_seq = 0;
161162 inode->i_rdev = 0;
162163 inode->dirtied_when = 0;
163164
....@@ -167,8 +168,6 @@
167168 inode->i_wb_frn_history = 0;
168169 #endif
169170
170
- if (security_inode_alloc(inode))
171
- goto out;
172171 spin_lock_init(&inode->i_lock);
173172 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
174173
....@@ -180,8 +179,13 @@
180179 mapping->a_ops = &empty_aops;
181180 mapping->host = inode;
182181 mapping->flags = 0;
182
+ if (sb->s_type->fs_flags & FS_THP_SUPPORT)
183
+ __set_bit(AS_THP_SUPPORT, &mapping->flags);
183184 mapping->wb_err = 0;
184185 atomic_set(&mapping->i_mmap_writable, 0);
186
+#ifdef CONFIG_READ_ONLY_THP_FOR_FS
187
+ atomic_set(&mapping->nr_thps, 0);
188
+#endif
185189 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
186190 mapping->private_data = NULL;
187191 mapping->writeback_index = 0;
....@@ -196,20 +200,37 @@
196200 inode->i_fsnotify_mask = 0;
197201 #endif
198202 inode->i_flctx = NULL;
203
+
204
+ if (unlikely(security_inode_alloc(inode)))
205
+ return -ENOMEM;
199206 this_cpu_inc(nr_inodes);
200207
201208 return 0;
202
-out:
203
- return -ENOMEM;
204209 }
205210 EXPORT_SYMBOL(inode_init_always);
206211
212
+void free_inode_nonrcu(struct inode *inode)
213
+{
214
+ kmem_cache_free(inode_cachep, inode);
215
+}
216
+EXPORT_SYMBOL(free_inode_nonrcu);
217
+
218
+static void i_callback(struct rcu_head *head)
219
+{
220
+ struct inode *inode = container_of(head, struct inode, i_rcu);
221
+ if (inode->free_inode)
222
+ inode->free_inode(inode);
223
+ else
224
+ free_inode_nonrcu(inode);
225
+}
226
+
207227 static struct inode *alloc_inode(struct super_block *sb)
208228 {
229
+ const struct super_operations *ops = sb->s_op;
209230 struct inode *inode;
210231
211
- if (sb->s_op->alloc_inode)
212
- inode = sb->s_op->alloc_inode(sb);
232
+ if (ops->alloc_inode)
233
+ inode = ops->alloc_inode(sb);
213234 else
214235 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
215236
....@@ -217,21 +238,18 @@
217238 return NULL;
218239
219240 if (unlikely(inode_init_always(sb, inode))) {
220
- if (inode->i_sb->s_op->destroy_inode)
221
- inode->i_sb->s_op->destroy_inode(inode);
222
- else
223
- kmem_cache_free(inode_cachep, inode);
241
+ if (ops->destroy_inode) {
242
+ ops->destroy_inode(inode);
243
+ if (!ops->free_inode)
244
+ return NULL;
245
+ }
246
+ inode->free_inode = ops->free_inode;
247
+ i_callback(&inode->i_rcu);
224248 return NULL;
225249 }
226250
227251 return inode;
228252 }
229
-
230
-void free_inode_nonrcu(struct inode *inode)
231
-{
232
- kmem_cache_free(inode_cachep, inode);
233
-}
234
-EXPORT_SYMBOL(free_inode_nonrcu);
235253
236254 void __destroy_inode(struct inode *inode)
237255 {
....@@ -255,20 +273,19 @@
255273 }
256274 EXPORT_SYMBOL(__destroy_inode);
257275
258
-static void i_callback(struct rcu_head *head)
259
-{
260
- struct inode *inode = container_of(head, struct inode, i_rcu);
261
- kmem_cache_free(inode_cachep, inode);
262
-}
263
-
264276 static void destroy_inode(struct inode *inode)
265277 {
278
+ const struct super_operations *ops = inode->i_sb->s_op;
279
+
266280 BUG_ON(!list_empty(&inode->i_lru));
267281 __destroy_inode(inode);
268
- if (inode->i_sb->s_op->destroy_inode)
269
- inode->i_sb->s_op->destroy_inode(inode);
270
- else
271
- call_rcu(&inode->i_rcu, i_callback);
282
+ if (ops->destroy_inode) {
283
+ ops->destroy_inode(inode);
284
+ if (!ops->free_inode)
285
+ return;
286
+ }
287
+ inode->free_inode = ops->free_inode;
288
+ call_rcu(&inode->i_rcu, i_callback);
272289 }
273290
274291 /**
....@@ -289,7 +306,7 @@
289306 if (!inode->i_nlink)
290307 atomic_long_inc(&inode->i_sb->s_remove_count);
291308 }
292
-EXPORT_SYMBOL(drop_nlink);
309
+EXPORT_SYMBOL_NS(drop_nlink, ANDROID_GKI_VFS_EXPORT_ONLY);
293310
294311 /**
295312 * clear_nlink - directly zero an inode's link count
....@@ -328,7 +345,7 @@
328345 inode->__i_nlink = nlink;
329346 }
330347 }
331
-EXPORT_SYMBOL(set_nlink);
348
+EXPORT_SYMBOL_NS(set_nlink, ANDROID_GKI_VFS_EXPORT_ONLY);
332349
333350 /**
334351 * inc_nlink - directly increment an inode's link count
....@@ -351,7 +368,7 @@
351368
352369 static void __address_space_init_once(struct address_space *mapping)
353370 {
354
- INIT_RADIX_TREE(&mapping->i_pages, GFP_ATOMIC | __GFP_ACCOUNT);
371
+ xa_init_flags(&mapping->i_pages, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT);
355372 init_rwsem(&mapping->i_mmap_rwsem);
356373 INIT_LIST_HEAD(&mapping->private_list);
357374 spin_lock_init(&mapping->private_lock);
....@@ -381,7 +398,7 @@
381398 __address_space_init_once(&inode->i_data);
382399 i_size_ordered_init(inode);
383400 }
384
-EXPORT_SYMBOL(inode_init_once);
401
+EXPORT_SYMBOL_NS(inode_init_once, ANDROID_GKI_VFS_EXPORT_ONLY);
385402
386403 static void init_once(void *foo)
387404 {
....@@ -405,7 +422,7 @@
405422 {
406423 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
407424 }
408
-EXPORT_SYMBOL(ihold);
425
+EXPORT_SYMBOL_NS(ihold, ANDROID_GKI_VFS_EXPORT_ONLY);
409426
410427 static void inode_lru_list_add(struct inode *inode)
411428 {
....@@ -481,11 +498,11 @@
481498
482499 spin_lock(&inode_hash_lock);
483500 spin_lock(&inode->i_lock);
484
- hlist_add_head(&inode->i_hash, b);
501
+ hlist_add_head_rcu(&inode->i_hash, b);
485502 spin_unlock(&inode->i_lock);
486503 spin_unlock(&inode_hash_lock);
487504 }
488
-EXPORT_SYMBOL(__insert_inode_hash);
505
+EXPORT_SYMBOL_NS(__insert_inode_hash, ANDROID_GKI_VFS_EXPORT_ONLY);
489506
490507 /**
491508 * __remove_inode_hash - remove an inode from the hash
....@@ -497,11 +514,11 @@
497514 {
498515 spin_lock(&inode_hash_lock);
499516 spin_lock(&inode->i_lock);
500
- hlist_del_init(&inode->i_hash);
517
+ hlist_del_init_rcu(&inode->i_hash);
501518 spin_unlock(&inode->i_lock);
502519 spin_unlock(&inode_hash_lock);
503520 }
504
-EXPORT_SYMBOL(__remove_inode_hash);
521
+EXPORT_SYMBOL_NS(__remove_inode_hash, ANDROID_GKI_VFS_EXPORT_ONLY);
505522
506523 void clear_inode(struct inode *inode)
507524 {
....@@ -521,7 +538,7 @@
521538 /* don't need i_lock here, no concurrent mods to i_state */
522539 inode->i_state = I_FREEING | I_CLEAR;
523540 }
524
-EXPORT_SYMBOL(clear_inode);
541
+EXPORT_SYMBOL_NS(clear_inode, ANDROID_GKI_VFS_EXPORT_ONLY);
525542
526543 /*
527544 * Free the inode passed in, removing it from the lists it is still connected
....@@ -983,7 +1000,7 @@
9831000 wake_up_bit(&inode->i_state, __I_NEW);
9841001 spin_unlock(&inode->i_lock);
9851002 }
986
-EXPORT_SYMBOL(unlock_new_inode);
1003
+EXPORT_SYMBOL_NS(unlock_new_inode, ANDROID_GKI_VFS_EXPORT_ONLY);
9871004
9881005 void discard_new_inode(struct inode *inode)
9891006 {
....@@ -1091,7 +1108,7 @@
10911108 */
10921109 spin_lock(&inode->i_lock);
10931110 inode->i_state |= I_NEW;
1094
- hlist_add_head(&inode->i_hash, head);
1111
+ hlist_add_head_rcu(&inode->i_hash, head);
10951112 spin_unlock(&inode->i_lock);
10961113 if (!creating)
10971114 inode_sb_list_add(inode);
....@@ -1140,7 +1157,7 @@
11401157 }
11411158 return inode;
11421159 }
1143
-EXPORT_SYMBOL(iget5_locked);
1160
+EXPORT_SYMBOL_NS(iget5_locked, ANDROID_GKI_VFS_EXPORT_ONLY);
11441161
11451162 /**
11461163 * iget_locked - obtain an inode from a mounted file system
....@@ -1185,7 +1202,7 @@
11851202 inode->i_ino = ino;
11861203 spin_lock(&inode->i_lock);
11871204 inode->i_state = I_NEW;
1188
- hlist_add_head(&inode->i_hash, head);
1205
+ hlist_add_head_rcu(&inode->i_hash, head);
11891206 spin_unlock(&inode->i_lock);
11901207 inode_sb_list_add(inode);
11911208 spin_unlock(&inode_hash_lock);
....@@ -1228,15 +1245,10 @@
12281245 struct hlist_head *b = inode_hashtable + hash(sb, ino);
12291246 struct inode *inode;
12301247
1231
- spin_lock(&inode_hash_lock);
1232
- hlist_for_each_entry(inode, b, i_hash) {
1233
- if (inode->i_ino == ino && inode->i_sb == sb) {
1234
- spin_unlock(&inode_hash_lock);
1248
+ hlist_for_each_entry_rcu(inode, b, i_hash) {
1249
+ if (inode->i_ino == ino && inode->i_sb == sb)
12351250 return 0;
1236
- }
12371251 }
1238
- spin_unlock(&inode_hash_lock);
1239
-
12401252 return 1;
12411253 }
12421254
....@@ -1265,6 +1277,7 @@
12651277 static unsigned int counter;
12661278 ino_t res;
12671279
1280
+ rcu_read_lock();
12681281 spin_lock(&iunique_lock);
12691282 do {
12701283 if (counter <= max_reserved)
....@@ -1272,10 +1285,11 @@
12721285 res = counter++;
12731286 } while (!test_inode_iunique(sb, res));
12741287 spin_unlock(&iunique_lock);
1288
+ rcu_read_unlock();
12751289
12761290 return res;
12771291 }
1278
-EXPORT_SYMBOL(iunique);
1292
+EXPORT_SYMBOL_NS(iunique, ANDROID_GKI_VFS_EXPORT_ONLY);
12791293
12801294 struct inode *igrab(struct inode *inode)
12811295 {
....@@ -1358,7 +1372,7 @@
13581372 }
13591373 return inode;
13601374 }
1361
-EXPORT_SYMBOL(ilookup5);
1375
+EXPORT_SYMBOL_NS(ilookup5, ANDROID_GKI_VFS_EXPORT_ONLY);
13621376
13631377 /**
13641378 * ilookup - search for an inode in the inode cache
....@@ -1440,6 +1454,84 @@
14401454 }
14411455 EXPORT_SYMBOL(find_inode_nowait);
14421456
1457
+/**
1458
+ * find_inode_rcu - find an inode in the inode cache
1459
+ * @sb: Super block of file system to search
1460
+ * @hashval: Key to hash
1461
+ * @test: Function to test match on an inode
1462
+ * @data: Data for test function
1463
+ *
1464
+ * Search for the inode specified by @hashval and @data in the inode cache,
1465
+ * where the helper function @test will return 0 if the inode does not match
1466
+ * and 1 if it does. The @test function must be responsible for taking the
1467
+ * i_lock spin_lock and checking i_state for an inode being freed or being
1468
+ * initialized.
1469
+ *
1470
+ * If successful, this will return the inode for which the @test function
1471
+ * returned 1 and NULL otherwise.
1472
+ *
1473
+ * The @test function is not permitted to take a ref on any inode presented.
1474
+ * It is also not permitted to sleep.
1475
+ *
1476
+ * The caller must hold the RCU read lock.
1477
+ */
1478
+struct inode *find_inode_rcu(struct super_block *sb, unsigned long hashval,
1479
+ int (*test)(struct inode *, void *), void *data)
1480
+{
1481
+ struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1482
+ struct inode *inode;
1483
+
1484
+ RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
1485
+ "suspicious find_inode_rcu() usage");
1486
+
1487
+ hlist_for_each_entry_rcu(inode, head, i_hash) {
1488
+ if (inode->i_sb == sb &&
1489
+ !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)) &&
1490
+ test(inode, data))
1491
+ return inode;
1492
+ }
1493
+ return NULL;
1494
+}
1495
+EXPORT_SYMBOL(find_inode_rcu);
1496
+
1497
+/**
1498
+ * find_inode_by_rcu - Find an inode in the inode cache
1499
+ * @sb: Super block of file system to search
1500
+ * @ino: The inode number to match
1501
+ *
1502
+ * Search for the inode specified by @hashval and @data in the inode cache,
1503
+ * where the helper function @test will return 0 if the inode does not match
1504
+ * and 1 if it does. The @test function must be responsible for taking the
1505
+ * i_lock spin_lock and checking i_state for an inode being freed or being
1506
+ * initialized.
1507
+ *
1508
+ * If successful, this will return the inode for which the @test function
1509
+ * returned 1 and NULL otherwise.
1510
+ *
1511
+ * The @test function is not permitted to take a ref on any inode presented.
1512
+ * It is also not permitted to sleep.
1513
+ *
1514
+ * The caller must hold the RCU read lock.
1515
+ */
1516
+struct inode *find_inode_by_ino_rcu(struct super_block *sb,
1517
+ unsigned long ino)
1518
+{
1519
+ struct hlist_head *head = inode_hashtable + hash(sb, ino);
1520
+ struct inode *inode;
1521
+
1522
+ RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
1523
+ "suspicious find_inode_by_ino_rcu() usage");
1524
+
1525
+ hlist_for_each_entry_rcu(inode, head, i_hash) {
1526
+ if (inode->i_ino == ino &&
1527
+ inode->i_sb == sb &&
1528
+ !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)))
1529
+ return inode;
1530
+ }
1531
+ return NULL;
1532
+}
1533
+EXPORT_SYMBOL(find_inode_by_ino_rcu);
1534
+
14431535 int insert_inode_locked(struct inode *inode)
14441536 {
14451537 struct super_block *sb = inode->i_sb;
....@@ -1464,7 +1556,7 @@
14641556 if (likely(!old)) {
14651557 spin_lock(&inode->i_lock);
14661558 inode->i_state |= I_NEW | I_CREATING;
1467
- hlist_add_head(&inode->i_hash, head);
1559
+ hlist_add_head_rcu(&inode->i_hash, head);
14681560 spin_unlock(&inode->i_lock);
14691561 spin_unlock(&inode_hash_lock);
14701562 return 0;
....@@ -1524,6 +1616,7 @@
15241616 {
15251617 struct super_block *sb = inode->i_sb;
15261618 const struct super_operations *op = inode->i_sb->s_op;
1619
+ unsigned long state;
15271620 int drop;
15281621
15291622 WARN_ON(inode->i_state & I_NEW);
....@@ -1533,22 +1626,28 @@
15331626 else
15341627 drop = generic_drop_inode(inode);
15351628
1536
- if (!drop && (sb->s_flags & SB_ACTIVE)) {
1629
+ if (!drop &&
1630
+ !(inode->i_state & I_DONTCACHE) &&
1631
+ (sb->s_flags & SB_ACTIVE)) {
15371632 inode_add_lru(inode);
15381633 spin_unlock(&inode->i_lock);
15391634 return;
15401635 }
15411636
1637
+ state = inode->i_state;
15421638 if (!drop) {
1543
- inode->i_state |= I_WILL_FREE;
1639
+ WRITE_ONCE(inode->i_state, state | I_WILL_FREE);
15441640 spin_unlock(&inode->i_lock);
1641
+
15451642 write_inode_now(inode, 1);
1643
+
15461644 spin_lock(&inode->i_lock);
1547
- WARN_ON(inode->i_state & I_NEW);
1548
- inode->i_state &= ~I_WILL_FREE;
1645
+ state = inode->i_state;
1646
+ WARN_ON(state & I_NEW);
1647
+ state &= ~I_WILL_FREE;
15491648 }
15501649
1551
- inode->i_state |= I_FREEING;
1650
+ WRITE_ONCE(inode->i_state, state | I_FREEING);
15521651 if (!list_empty(&inode->i_lru))
15531652 inode_lru_list_del(inode);
15541653 spin_unlock(&inode->i_lock);
....@@ -1584,25 +1683,31 @@
15841683 }
15851684 EXPORT_SYMBOL(iput);
15861685
1686
+#ifdef CONFIG_BLOCK
15871687 /**
15881688 * bmap - find a block number in a file
1589
- * @inode: inode of file
1590
- * @block: block to find
1689
+ * @inode: inode owning the block number being requested
1690
+ * @block: pointer containing the block to find
15911691 *
1592
- * Returns the block number on the device holding the inode that
1593
- * is the disk block number for the block of the file requested.
1594
- * That is, asked for block 4 of inode 1 the function will return the
1595
- * disk block relative to the disk start that holds that block of the
1596
- * file.
1692
+ * Replaces the value in ``*block`` with the block number on the device holding
1693
+ * corresponding to the requested block number in the file.
1694
+ * That is, asked for block 4 of inode 1 the function will replace the
1695
+ * 4 in ``*block``, with disk block relative to the disk start that holds that
1696
+ * block of the file.
1697
+ *
1698
+ * Returns -EINVAL in case of error, 0 otherwise. If mapping falls into a
1699
+ * hole, returns 0 and ``*block`` is also set to 0.
15971700 */
1598
-sector_t bmap(struct inode *inode, sector_t block)
1701
+int bmap(struct inode *inode, sector_t *block)
15991702 {
1600
- sector_t res = 0;
1601
- if (inode->i_mapping->a_ops->bmap)
1602
- res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1603
- return res;
1703
+ if (!inode->i_mapping->a_ops->bmap)
1704
+ return -EINVAL;
1705
+
1706
+ *block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block);
1707
+ return 0;
16041708 }
16051709 EXPORT_SYMBOL(bmap);
1710
+#endif
16061711
16071712 /*
16081713 * With relative atime, only update atime if the previous atime is
....@@ -1610,7 +1715,7 @@
16101715 * passed since the last atime update.
16111716 */
16121717 static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1613
- struct timespec now)
1718
+ struct timespec64 now)
16141719 {
16151720
16161721 if (!(mnt->mnt_flags & MNT_RELATIME))
....@@ -1666,15 +1771,13 @@
16661771 * This does the actual work of updating an inodes time or version. Must have
16671772 * had called mnt_want_write() before calling this.
16681773 */
1669
-static int update_time(struct inode *inode, struct timespec64 *time, int flags)
1774
+int inode_update_time(struct inode *inode, struct timespec64 *time, int flags)
16701775 {
1671
- int (*update_time)(struct inode *, struct timespec64 *, int);
1672
-
1673
- update_time = inode->i_op->update_time ? inode->i_op->update_time :
1674
- generic_update_time;
1675
-
1676
- return update_time(inode, time, flags);
1776
+ if (inode->i_op->update_time)
1777
+ return inode->i_op->update_time(inode, time, flags);
1778
+ return generic_update_time(inode, time, flags);
16771779 }
1780
+EXPORT_SYMBOL(inode_update_time);
16781781
16791782 /**
16801783 * touch_atime - update the access time
....@@ -1711,7 +1814,7 @@
17111814
17121815 now = current_time(inode);
17131816
1714
- if (!relatime_need_update(mnt, inode, timespec64_to_timespec(now)))
1817
+ if (!relatime_need_update(mnt, inode, now))
17151818 return false;
17161819
17171820 if (timespec64_equal(&inode->i_atime, &now))
....@@ -1744,12 +1847,12 @@
17441847 * of the fs read only, e.g. subvolumes in Btrfs.
17451848 */
17461849 now = current_time(inode);
1747
- update_time(inode, &now, S_ATIME);
1850
+ inode_update_time(inode, &now, S_ATIME);
17481851 __mnt_drop_write(mnt);
17491852 skip_update:
17501853 sb_end_write(inode->i_sb);
17511854 }
1752
-EXPORT_SYMBOL(touch_atime);
1855
+EXPORT_SYMBOL_NS(touch_atime, ANDROID_GKI_VFS_EXPORT_ONLY);
17531856
17541857 /*
17551858 * The logic we want is
....@@ -1803,7 +1906,7 @@
18031906 return mask;
18041907 }
18051908
1806
-static int __remove_privs(struct vfsmount *mnt, struct dentry *dentry, int kill)
1909
+static int __remove_privs(struct dentry *dentry, int kill)
18071910 {
18081911 struct iattr newattrs;
18091912
....@@ -1812,7 +1915,7 @@
18121915 * Note we call this on write, so notify_change will not
18131916 * encounter any conflicting delegations:
18141917 */
1815
- return notify_change2(mnt, dentry, &newattrs, NULL);
1918
+ return notify_change(dentry, &newattrs, NULL);
18161919 }
18171920
18181921 /*
....@@ -1839,13 +1942,13 @@
18391942 if (kill < 0)
18401943 return kill;
18411944 if (kill)
1842
- error = __remove_privs(file->f_path.mnt, dentry, kill);
1945
+ error = __remove_privs(dentry, kill);
18431946 if (!error)
18441947 inode_has_no_xattr(inode);
18451948
18461949 return error;
18471950 }
1848
-EXPORT_SYMBOL(file_remove_privs);
1951
+EXPORT_SYMBOL_NS(file_remove_privs, ANDROID_GKI_VFS_EXPORT_ONLY);
18491952
18501953 /**
18511954 * file_update_time - update mtime and ctime time
....@@ -1888,12 +1991,32 @@
18881991 if (__mnt_want_write_file(file))
18891992 return 0;
18901993
1891
- ret = update_time(inode, &now, sync_it);
1994
+ ret = inode_update_time(inode, &now, sync_it);
18921995 __mnt_drop_write_file(file);
18931996
18941997 return ret;
18951998 }
18961999 EXPORT_SYMBOL(file_update_time);
2000
+
2001
+/* Caller must hold the file's inode lock */
2002
+int file_modified(struct file *file)
2003
+{
2004
+ int err;
2005
+
2006
+ /*
2007
+ * Clear the security bits if the process is not being run by root.
2008
+ * This keeps people from modifying setuid and setgid binaries.
2009
+ */
2010
+ err = file_remove_privs(file);
2011
+ if (err)
2012
+ return err;
2013
+
2014
+ if (unlikely(file->f_mode & FMODE_NOCMTIME))
2015
+ return 0;
2016
+
2017
+ return file_update_time(file);
2018
+}
2019
+EXPORT_SYMBOL(file_modified);
18972020
18982021 int inode_needs_sync(struct inode *inode)
18992022 {
....@@ -2006,7 +2129,7 @@
20062129 " inode %s:%lu\n", mode, inode->i_sb->s_id,
20072130 inode->i_ino);
20082131 }
2009
-EXPORT_SYMBOL(init_special_inode);
2132
+EXPORT_SYMBOL_NS(init_special_inode, ANDROID_GKI_VFS_EXPORT_ONLY);
20102133
20112134 /**
20122135 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
....@@ -2032,7 +2155,7 @@
20322155 inode->i_gid = current_fsgid();
20332156 inode->i_mode = mode;
20342157 }
2035
-EXPORT_SYMBOL(inode_init_owner);
2158
+EXPORT_SYMBOL_NS(inode_init_owner, ANDROID_GKI_VFS_EXPORT_ONLY);
20362159
20372160 /**
20382161 * inode_owner_or_capable - check current task permissions to inode
....@@ -2086,7 +2209,7 @@
20862209 if (atomic_read(&inode->i_dio_count))
20872210 __inode_dio_wait(inode);
20882211 }
2089
-EXPORT_SYMBOL(inode_dio_wait);
2212
+EXPORT_SYMBOL_NS(inode_dio_wait, ANDROID_GKI_VFS_EXPORT_ONLY);
20902213
20912214 /*
20922215 * inode_set_flags - atomically set some inode flags
....@@ -2107,16 +2230,10 @@
21072230 void inode_set_flags(struct inode *inode, unsigned int flags,
21082231 unsigned int mask)
21092232 {
2110
- unsigned int old_flags, new_flags;
2111
-
21122233 WARN_ON_ONCE(flags & ~mask);
2113
- do {
2114
- old_flags = READ_ONCE(inode->i_flags);
2115
- new_flags = (old_flags & ~mask) | flags;
2116
- } while (unlikely(cmpxchg(&inode->i_flags, old_flags,
2117
- new_flags) != old_flags));
2234
+ set_mask_bits(&inode->i_flags, mask, flags);
21182235 }
2119
-EXPORT_SYMBOL(inode_set_flags);
2236
+EXPORT_SYMBOL_NS(inode_set_flags, ANDROID_GKI_VFS_EXPORT_ONLY);
21202237
21212238 void inode_nohighmem(struct inode *inode)
21222239 {
....@@ -2125,28 +2242,35 @@
21252242 EXPORT_SYMBOL(inode_nohighmem);
21262243
21272244 /**
2128
- * timespec64_trunc - Truncate timespec64 to a granularity
2129
- * @t: Timespec64
2130
- * @gran: Granularity in ns.
2245
+ * timestamp_truncate - Truncate timespec to a granularity
2246
+ * @t: Timespec
2247
+ * @inode: inode being updated
21312248 *
2132
- * Truncate a timespec64 to a granularity. Always rounds down. gran must
2249
+ * Truncate a timespec to the granularity supported by the fs
2250
+ * containing the inode. Always rounds down. gran must
21332251 * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
21342252 */
2135
-struct timespec64 timespec64_trunc(struct timespec64 t, unsigned gran)
2253
+struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode)
21362254 {
2137
- /* Avoid division in the common cases 1 ns and 1 s. */
2138
- if (gran == 1) {
2139
- /* nothing */
2140
- } else if (gran == NSEC_PER_SEC) {
2255
+ struct super_block *sb = inode->i_sb;
2256
+ unsigned int gran = sb->s_time_gran;
2257
+
2258
+ t.tv_sec = clamp(t.tv_sec, sb->s_time_min, sb->s_time_max);
2259
+ if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min))
21412260 t.tv_nsec = 0;
2142
- } else if (gran > 1 && gran < NSEC_PER_SEC) {
2261
+
2262
+ /* Avoid division in the common cases 1 ns and 1 s. */
2263
+ if (gran == 1)
2264
+ ; /* nothing */
2265
+ else if (gran == NSEC_PER_SEC)
2266
+ t.tv_nsec = 0;
2267
+ else if (gran > 1 && gran < NSEC_PER_SEC)
21432268 t.tv_nsec -= t.tv_nsec % gran;
2144
- } else {
2145
- WARN(1, "illegal file time granularity: %u", gran);
2146
- }
2269
+ else
2270
+ WARN(1, "invalid file time granularity: %u", gran);
21472271 return t;
21482272 }
2149
-EXPORT_SYMBOL(timespec64_trunc);
2273
+EXPORT_SYMBOL_NS(timestamp_truncate, ANDROID_GKI_VFS_EXPORT_ONLY);
21502274
21512275 /**
21522276 * current_time - Return FS time
....@@ -2160,14 +2284,16 @@
21602284 */
21612285 struct timespec64 current_time(struct inode *inode)
21622286 {
2163
- struct timespec64 now = current_kernel_time64();
2287
+ struct timespec64 now;
2288
+
2289
+ ktime_get_coarse_real_ts64(&now);
21642290
21652291 if (unlikely(!inode->i_sb)) {
21662292 WARN(1, "current_time() called with uninitialized super_block in the inode");
21672293 return now;
21682294 }
21692295
2170
- return timespec64_trunc(now, inode->i_sb->s_time_gran);
2296
+ return timestamp_truncate(now, inode);
21712297 }
21722298 EXPORT_SYMBOL(current_time);
21732299