hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
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",
....@@ -442,6 +426,7 @@
442426 mutex_unlock(&dir_ui->ui_mutex);
443427
444428 ubifs_release_budget(c, &req);
429
+ fscrypt_free_filename(&nm);
445430
446431 return 0;
447432
....@@ -451,6 +436,8 @@
451436 make_bad_inode(inode);
452437 if (!instantiated)
453438 iput(inode);
439
+ else if (whiteout)
440
+ iput(*whiteout);
454441 out_budg:
455442 ubifs_release_budget(c, &req);
456443 if (!instantiated)
....@@ -522,7 +509,7 @@
522509 struct ubifs_dent_node *dent;
523510 struct inode *dir = file_inode(file);
524511 struct ubifs_info *c = dir->i_sb->s_fs_info;
525
- bool encrypted = ubifs_crypt_is_encrypted(dir);
512
+ bool encrypted = IS_ENCRYPTED(dir);
526513
527514 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
528515
....@@ -534,11 +521,11 @@
534521 return 0;
535522
536523 if (encrypted) {
537
- err = fscrypt_get_encryption_info(dir);
524
+ err = fscrypt_prepare_readdir(dir);
538525 if (err)
539526 return err;
540527
541
- err = fscrypt_fname_alloc_buffer(dir, UBIFS_MAX_NLEN, &fstr);
528
+ err = fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN, &fstr);
542529 if (err)
543530 return err;
544531
....@@ -801,13 +788,11 @@
801788 dentry, inode->i_ino,
802789 inode->i_nlink, dir->i_ino);
803790
804
- if (ubifs_crypt_is_encrypted(dir)) {
805
- err = fscrypt_get_encryption_info(dir);
806
- if (err && err != -ENOKEY)
807
- return err;
808
- }
809
-
810791 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
792
+ if (err)
793
+ return err;
794
+
795
+ err = ubifs_purge_xattrs(inode);
811796 if (err)
812797 return err;
813798
....@@ -911,13 +896,11 @@
911896 if (err)
912897 return err;
913898
914
- if (ubifs_crypt_is_encrypted(dir)) {
915
- err = fscrypt_get_encryption_info(dir);
916
- if (err && err != -ENOKEY)
917
- return err;
918
- }
919
-
920899 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
900
+ if (err)
901
+ return err;
902
+
903
+ err = ubifs_purge_xattrs(inode);
921904 if (err)
922905 return err;
923906
....@@ -971,7 +954,8 @@
971954 struct ubifs_inode *dir_ui = ubifs_inode(dir);
972955 struct ubifs_info *c = dir->i_sb->s_fs_info;
973956 int err, sz_change;
974
- struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
957
+ struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
958
+ .dirtied_ino = 1};
975959 struct fscrypt_name nm;
976960
977961 /*
....@@ -1135,7 +1119,6 @@
11351119 int err, sz_change, len = strlen(symname);
11361120 struct fscrypt_str disk_link;
11371121 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1138
- .new_ino_d = ALIGN(len, 8),
11391122 .dirtied_ino = 1 };
11401123 struct fscrypt_name nm;
11411124
....@@ -1151,6 +1134,7 @@
11511134 * Budget request settings: new inode, new direntry and changing parent
11521135 * directory inode.
11531136 */
1137
+ req.new_ino_d = ALIGN(disk_link.len - 1, 8);
11541138 err = ubifs_budget_space(c, &req);
11551139 if (err)
11561140 return err;
....@@ -1287,7 +1271,7 @@
12871271 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
12881272 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
12891273 struct timespec64 time;
1290
- unsigned int uninitialized_var(saved_nlink);
1274
+ unsigned int saved_nlink;
12911275 struct fscrypt_name old_nm, new_nm;
12921276
12931277 /*
....@@ -1303,8 +1287,15 @@
13031287 old_dentry, old_inode->i_ino, old_dir->i_ino,
13041288 new_dentry, new_dir->i_ino, flags);
13051289
1306
- if (unlink)
1290
+ if (unlink) {
13071291 ubifs_assert(c, inode_is_locked(new_inode));
1292
+
1293
+ /* Budget for old inode's data when its nlink > 1. */
1294
+ req.dirtied_ino_d = ALIGN(ubifs_inode(new_inode)->data_len, 8);
1295
+ err = ubifs_purge_xattrs(new_inode);
1296
+ if (err)
1297
+ return err;
1298
+ }
13081299
13091300 if (unlink && is_dir) {
13101301 err = ubifs_check_dir_empty(new_inode);
....@@ -1341,6 +1332,7 @@
13411332
13421333 if (flags & RENAME_WHITEOUT) {
13431334 union ubifs_dev_desc *dev = NULL;
1335
+ struct ubifs_budget_req wht_req;
13441336
13451337 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
13461338 if (!dev) {
....@@ -1362,6 +1354,23 @@
13621354 whiteout_ui->data = dev;
13631355 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
13641356 ubifs_assert(c, !whiteout_ui->dirty);
1357
+
1358
+ memset(&wht_req, 0, sizeof(struct ubifs_budget_req));
1359
+ wht_req.dirtied_ino = 1;
1360
+ wht_req.dirtied_ino_d = ALIGN(whiteout_ui->data_len, 8);
1361
+ /*
1362
+ * To avoid deadlock between space budget (holds ui_mutex and
1363
+ * waits wb work) and writeback work(waits ui_mutex), do space
1364
+ * budget before ubifs inodes locked.
1365
+ */
1366
+ err = ubifs_budget_space(c, &wht_req);
1367
+ if (err) {
1368
+ iput(whiteout);
1369
+ goto out_release;
1370
+ }
1371
+
1372
+ /* Add the old_dentry size to the old_dir size. */
1373
+ old_sz -= CALC_DENT_SIZE(fname_len(&old_nm));
13651374 }
13661375
13671376 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
....@@ -1436,18 +1445,6 @@
14361445 }
14371446
14381447 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
-
14511448 inc_nlink(whiteout);
14521449 mark_inode_dirty(whiteout);
14531450
....@@ -1537,6 +1534,10 @@
15371534 return err;
15381535 }
15391536
1537
+ err = ubifs_budget_space(c, &req);
1538
+ if (err)
1539
+ goto out;
1540
+
15401541 lock_4_inodes(old_dir, new_dir, NULL, NULL);
15411542
15421543 time = current_time(old_dir);
....@@ -1562,6 +1563,7 @@
15621563 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
15631564 ubifs_release_budget(c, &req);
15641565
1566
+out:
15651567 fscrypt_free_filename(&fst_nm);
15661568 fscrypt_free_filename(&snd_nm);
15671569 return err;
....@@ -1645,14 +1647,6 @@
16451647 return 0;
16461648 }
16471649
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
-
16561650 const struct inode_operations ubifs_dir_inode_operations = {
16571651 .lookup = ubifs_lookup,
16581652 .create = ubifs_create,
....@@ -1668,9 +1662,7 @@
16681662 #ifdef CONFIG_UBIFS_FS_XATTR
16691663 .listxattr = ubifs_listxattr,
16701664 #endif
1671
-#ifdef CONFIG_UBIFS_ATIME_SUPPORT
16721665 .update_time = ubifs_update_time,
1673
-#endif
16741666 .tmpfile = ubifs_tmpfile,
16751667 };
16761668
....@@ -1681,24 +1673,7 @@
16811673 .iterate_shared = ubifs_readdir,
16821674 .fsync = ubifs_fsync,
16831675 .unlocked_ioctl = ubifs_ioctl,
1684
- .open = ubifs_dir_open,
16851676 #ifdef CONFIG_COMPAT
16861677 .compat_ioctl = ubifs_compat_ioctl,
16871678 #endif
16881679 };
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
-}