forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/fs/ubifs/dir.c
....@@ -1,20 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* * This file is part of UBIFS.
23 *
34 * Copyright (C) 2006-2008 Nokia Corporation.
45 * Copyright (C) 2006, 2007 University of Szeged, Hungary
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms of the GNU General Public License version 2 as published by
8
- * the Free Software Foundation.
9
- *
10
- * This program is distributed in the hope that it will be useful, but WITHOUT
11
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
- * more details.
14
- *
15
- * You should have received a copy of the GNU General Public License along with
16
- * this program; if not, write to the Free Software Foundation, Inc., 51
17
- * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
186 *
197 * Authors: Artem Bityutskiy (Битюцкий Артём)
208 * Adrian Hunter
....@@ -93,19 +81,6 @@
9381 struct ubifs_inode *ui;
9482 bool encrypted = false;
9583
96
- if (ubifs_crypt_is_encrypted(dir)) {
97
- err = fscrypt_get_encryption_info(dir);
98
- if (err) {
99
- ubifs_err(c, "fscrypt_get_encryption_info failed: %i", err);
100
- return ERR_PTR(err);
101
- }
102
-
103
- if (!fscrypt_has_encryption_key(dir))
104
- return ERR_PTR(-EPERM);
105
-
106
- encrypted = true;
107
- }
108
-
10984 inode = new_inode(c->vfs_sb);
11085 ui = ubifs_inode(inode);
11186 if (!inode)
....@@ -123,6 +98,12 @@
12398 inode->i_mtime = inode->i_atime = inode->i_ctime =
12499 current_time(inode);
125100 inode->i_mapping->nrpages = 0;
101
+
102
+ err = fscrypt_prepare_new_inode(dir, inode, &encrypted);
103
+ if (err) {
104
+ ubifs_err(c, "fscrypt_prepare_new_inode failed: %i", err);
105
+ goto out_iput;
106
+ }
126107
127108 switch (mode & S_IFMT) {
128109 case S_IFREG:
....@@ -143,7 +124,6 @@
143124 case S_IFBLK:
144125 case S_IFCHR:
145126 inode->i_op = &ubifs_file_inode_operations;
146
- encrypted = false;
147127 break;
148128 default:
149129 BUG();
....@@ -163,9 +143,8 @@
163143 if (c->highest_inum >= INUM_WATERMARK) {
164144 spin_unlock(&c->cnt_lock);
165145 ubifs_err(c, "out of inode numbers");
166
- make_bad_inode(inode);
167
- iput(inode);
168
- return ERR_PTR(-EINVAL);
146
+ err = -EINVAL;
147
+ goto out_iput;
169148 }
170149 ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
171150 (unsigned long)c->highest_inum, INUM_WATERMARK);
....@@ -183,16 +162,19 @@
183162 spin_unlock(&c->cnt_lock);
184163
185164 if (encrypted) {
186
- err = fscrypt_inherit_context(dir, inode, &encrypted, true);
165
+ err = fscrypt_set_context(inode, NULL);
187166 if (err) {
188
- ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
189
- make_bad_inode(inode);
190
- iput(inode);
191
- return ERR_PTR(err);
167
+ ubifs_err(c, "fscrypt_set_context failed: %i", err);
168
+ goto out_iput;
192169 }
193170 }
194171
195172 return inode;
173
+
174
+out_iput:
175
+ make_bad_inode(inode);
176
+ iput(inode);
177
+ return ERR_PTR(err);
196178 }
197179
198180 static int dbg_check_name(const struct ubifs_info *c,
....@@ -208,7 +190,6 @@
208190 return 0;
209191 }
210192
211
-static void ubifs_set_d_ops(struct inode *dir, struct dentry *dentry);
212193 static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
213194 unsigned int flags)
214195 {
....@@ -222,7 +203,7 @@
222203 dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
223204
224205 err = fscrypt_prepare_lookup(dir, dentry, &nm);
225
- ubifs_set_d_ops(dir, dentry);
206
+ generic_set_encrypted_ci_d_ops(dentry);
226207 if (err == -ENOENT)
227208 return d_splice_alias(NULL, dentry);
228209 if (err)
....@@ -275,7 +256,7 @@
275256 goto done;
276257 }
277258
278
- if (ubifs_crypt_is_encrypted(dir) &&
259
+ if (IS_ENCRYPTED(dir) &&
279260 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
280261 !fscrypt_has_permitted_context(dir, inode)) {
281262 ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
....@@ -373,15 +354,18 @@
373354 {
374355 struct inode *inode;
375356 struct ubifs_info *c = dir->i_sb->s_fs_info;
376
- struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
357
+ struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
358
+ .dirtied_ino = 1};
377359 struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
378360 struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
379361 int err, instantiated = 0;
380362 struct fscrypt_name nm;
381363
382364 /*
383
- * Budget request settings: new dirty inode, new direntry,
384
- * budget for dirtied inode will be released via writeback.
365
+ * Budget request settings: new inode, new direntry, changing the
366
+ * parent directory inode.
367
+ * Allocate budget separately for new dirtied inode, the budget will
368
+ * be released via writeback.
385369 */
386370
387371 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
....@@ -451,6 +435,8 @@
451435 make_bad_inode(inode);
452436 if (!instantiated)
453437 iput(inode);
438
+ else if (whiteout)
439
+ iput(*whiteout);
454440 out_budg:
455441 ubifs_release_budget(c, &req);
456442 if (!instantiated)
....@@ -522,7 +508,7 @@
522508 struct ubifs_dent_node *dent;
523509 struct inode *dir = file_inode(file);
524510 struct ubifs_info *c = dir->i_sb->s_fs_info;
525
- bool encrypted = ubifs_crypt_is_encrypted(dir);
511
+ bool encrypted = IS_ENCRYPTED(dir);
526512
527513 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
528514
....@@ -534,11 +520,11 @@
534520 return 0;
535521
536522 if (encrypted) {
537
- err = fscrypt_get_encryption_info(dir);
523
+ err = fscrypt_prepare_readdir(dir);
538524 if (err)
539525 return err;
540526
541
- err = fscrypt_fname_alloc_buffer(dir, UBIFS_MAX_NLEN, &fstr);
527
+ err = fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN, &fstr);
542528 if (err)
543529 return err;
544530
....@@ -801,13 +787,11 @@
801787 dentry, inode->i_ino,
802788 inode->i_nlink, dir->i_ino);
803789
804
- if (ubifs_crypt_is_encrypted(dir)) {
805
- err = fscrypt_get_encryption_info(dir);
806
- if (err && err != -ENOKEY)
807
- return err;
808
- }
809
-
810790 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
791
+ if (err)
792
+ return err;
793
+
794
+ err = ubifs_purge_xattrs(inode);
811795 if (err)
812796 return err;
813797
....@@ -911,13 +895,11 @@
911895 if (err)
912896 return err;
913897
914
- if (ubifs_crypt_is_encrypted(dir)) {
915
- err = fscrypt_get_encryption_info(dir);
916
- if (err && err != -ENOKEY)
917
- return err;
918
- }
919
-
920898 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
899
+ if (err)
900
+ return err;
901
+
902
+ err = ubifs_purge_xattrs(inode);
921903 if (err)
922904 return err;
923905
....@@ -971,7 +953,8 @@
971953 struct ubifs_inode *dir_ui = ubifs_inode(dir);
972954 struct ubifs_info *c = dir->i_sb->s_fs_info;
973955 int err, sz_change;
974
- struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
956
+ struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
957
+ .dirtied_ino = 1};
975958 struct fscrypt_name nm;
976959
977960 /*
....@@ -1287,7 +1270,7 @@
12871270 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
12881271 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
12891272 struct timespec64 time;
1290
- unsigned int uninitialized_var(saved_nlink);
1273
+ unsigned int saved_nlink;
12911274 struct fscrypt_name old_nm, new_nm;
12921275
12931276 /*
....@@ -1303,8 +1286,13 @@
13031286 old_dentry, old_inode->i_ino, old_dir->i_ino,
13041287 new_dentry, new_dir->i_ino, flags);
13051288
1306
- if (unlink)
1289
+ if (unlink) {
13071290 ubifs_assert(c, inode_is_locked(new_inode));
1291
+
1292
+ err = ubifs_purge_xattrs(new_inode);
1293
+ if (err)
1294
+ return err;
1295
+ }
13081296
13091297 if (unlink && is_dir) {
13101298 err = ubifs_check_dir_empty(new_inode);
....@@ -1341,6 +1329,7 @@
13411329
13421330 if (flags & RENAME_WHITEOUT) {
13431331 union ubifs_dev_desc *dev = NULL;
1332
+ struct ubifs_budget_req wht_req;
13441333
13451334 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
13461335 if (!dev) {
....@@ -1362,6 +1351,23 @@
13621351 whiteout_ui->data = dev;
13631352 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
13641353 ubifs_assert(c, !whiteout_ui->dirty);
1354
+
1355
+ memset(&wht_req, 0, sizeof(struct ubifs_budget_req));
1356
+ wht_req.dirtied_ino = 1;
1357
+ wht_req.dirtied_ino_d = ALIGN(whiteout_ui->data_len, 8);
1358
+ /*
1359
+ * To avoid deadlock between space budget (holds ui_mutex and
1360
+ * waits wb work) and writeback work(waits ui_mutex), do space
1361
+ * budget before ubifs inodes locked.
1362
+ */
1363
+ err = ubifs_budget_space(c, &wht_req);
1364
+ if (err) {
1365
+ iput(whiteout);
1366
+ goto out_release;
1367
+ }
1368
+
1369
+ /* Add the old_dentry size to the old_dir size. */
1370
+ old_sz -= CALC_DENT_SIZE(fname_len(&old_nm));
13651371 }
13661372
13671373 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
....@@ -1436,18 +1442,6 @@
14361442 }
14371443
14381444 if (whiteout) {
1439
- struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1440
- .dirtied_ino_d = \
1441
- ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1442
-
1443
- err = ubifs_budget_space(c, &wht_req);
1444
- if (err) {
1445
- kfree(whiteout_ui->data);
1446
- whiteout_ui->data_len = 0;
1447
- iput(whiteout);
1448
- goto out_release;
1449
- }
1450
-
14511445 inc_nlink(whiteout);
14521446 mark_inode_dirty(whiteout);
14531447
....@@ -1645,14 +1639,6 @@
16451639 return 0;
16461640 }
16471641
1648
-static int ubifs_dir_open(struct inode *dir, struct file *file)
1649
-{
1650
- if (ubifs_crypt_is_encrypted(dir))
1651
- return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1652
-
1653
- return 0;
1654
-}
1655
-
16561642 const struct inode_operations ubifs_dir_inode_operations = {
16571643 .lookup = ubifs_lookup,
16581644 .create = ubifs_create,
....@@ -1668,9 +1654,7 @@
16681654 #ifdef CONFIG_UBIFS_FS_XATTR
16691655 .listxattr = ubifs_listxattr,
16701656 #endif
1671
-#ifdef CONFIG_UBIFS_ATIME_SUPPORT
16721657 .update_time = ubifs_update_time,
1673
-#endif
16741658 .tmpfile = ubifs_tmpfile,
16751659 };
16761660
....@@ -1681,24 +1665,7 @@
16811665 .iterate_shared = ubifs_readdir,
16821666 .fsync = ubifs_fsync,
16831667 .unlocked_ioctl = ubifs_ioctl,
1684
- .open = ubifs_dir_open,
16851668 #ifdef CONFIG_COMPAT
16861669 .compat_ioctl = ubifs_compat_ioctl,
16871670 #endif
16881671 };
1689
-
1690
-#ifdef CONFIG_FS_ENCRYPTION
1691
-static const struct dentry_operations ubifs_encrypted_dentry_ops = {
1692
- .d_revalidate = fscrypt_d_revalidate,
1693
-};
1694
-#endif
1695
-
1696
-static void ubifs_set_d_ops(struct inode *dir, struct dentry *dentry)
1697
-{
1698
-#ifdef CONFIG_FS_ENCRYPTION
1699
- if (dentry->d_flags & DCACHE_ENCRYPTED_NAME) {
1700
- d_set_d_op(dentry, &ubifs_encrypted_dentry_ops);
1701
- return;
1702
- }
1703
-#endif
1704
-}