@@ -71,8 +71,13 @@ static int inherit_flags(const struct inode *dir, umode_t mode)
71
71
* @is_xattr: whether the inode is xattr inode
72
72
*
73
73
* This function finds an unused inode number, allocates new inode and
74
- * initializes it. Returns new inode in case of success and an error code in
75
- * case of failure.
74
+ * initializes it. Non-xattr new inode may be written with xattrs(selinux/
75
+ * encryption) before writing dentry, which could cause inconsistent problem
76
+ * when powercut happens between two operations. To deal with it, non-xattr
77
+ * new inode is initialized with zero-nlink and added into orphan list, caller
78
+ * should make sure that inode is relinked later, and make sure that orphan
79
+ * removing and journal writing into an committing atomic operation. Returns
80
+ * new inode in case of success and an error code in case of failure.
76
81
*/
77
82
struct inode * ubifs_new_inode (struct ubifs_info * c , struct inode * dir ,
78
83
umode_t mode , bool is_xattr )
@@ -163,9 +168,25 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
163
168
ui -> creat_sqnum = ++ c -> max_sqnum ;
164
169
spin_unlock (& c -> cnt_lock );
165
170
171
+ if (!is_xattr ) {
172
+ set_nlink (inode , 0 );
173
+ err = ubifs_add_orphan (c , inode -> i_ino );
174
+ if (err ) {
175
+ ubifs_err (c , "ubifs_add_orphan failed: %i" , err );
176
+ goto out_iput ;
177
+ }
178
+ down_read (& c -> commit_sem );
179
+ ui -> del_cmtno = c -> cmt_no ;
180
+ up_read (& c -> commit_sem );
181
+ }
182
+
166
183
if (encrypted ) {
167
184
err = fscrypt_set_context (inode , NULL );
168
185
if (err ) {
186
+ if (!is_xattr ) {
187
+ set_nlink (inode , 1 );
188
+ ubifs_delete_orphan (c , inode -> i_ino );
189
+ }
169
190
ubifs_err (c , "fscrypt_set_context failed: %i" , err );
170
191
goto out_iput ;
171
192
}
@@ -320,12 +341,13 @@ static int ubifs_create(struct mnt_idmap *idmap, struct inode *dir,
320
341
if (err )
321
342
goto out_inode ;
322
343
344
+ set_nlink (inode , 1 );
323
345
mutex_lock (& dir_ui -> ui_mutex );
324
346
dir -> i_size += sz_change ;
325
347
dir_ui -> ui_size = dir -> i_size ;
326
348
inode_set_mtime_to_ts (dir ,
327
349
inode_set_ctime_to_ts (dir , inode_get_ctime (inode )));
328
- err = ubifs_jnl_update (c , dir , & nm , inode , 0 , 0 , 0 );
350
+ err = ubifs_jnl_update (c , dir , & nm , inode , 0 , 0 , 1 );
329
351
if (err )
330
352
goto out_cancel ;
331
353
mutex_unlock (& dir_ui -> ui_mutex );
@@ -340,8 +362,8 @@ static int ubifs_create(struct mnt_idmap *idmap, struct inode *dir,
340
362
dir -> i_size -= sz_change ;
341
363
dir_ui -> ui_size = dir -> i_size ;
342
364
mutex_unlock (& dir_ui -> ui_mutex );
365
+ set_nlink (inode , 0 );
343
366
out_inode :
344
- make_bad_inode (inode );
345
367
iput (inode );
346
368
out_fname :
347
369
fscrypt_free_filename (& nm );
@@ -386,7 +408,6 @@ static struct inode *create_whiteout(struct inode *dir, struct dentry *dentry)
386
408
return inode ;
387
409
388
410
out_inode :
389
- make_bad_inode (inode );
390
411
iput (inode );
391
412
out_free :
392
413
ubifs_err (c , "cannot create whiteout file, error %d" , err );
@@ -470,6 +491,7 @@ static int ubifs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
470
491
if (err )
471
492
goto out_inode ;
472
493
494
+ set_nlink (inode , 1 );
473
495
mutex_lock (& ui -> ui_mutex );
474
496
insert_inode_hash (inode );
475
497
d_tmpfile (file , inode );
@@ -479,7 +501,7 @@ static int ubifs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
479
501
mutex_unlock (& ui -> ui_mutex );
480
502
481
503
lock_2_inodes (dir , inode );
482
- err = ubifs_jnl_update (c , dir , & nm , inode , 1 , 0 , 0 );
504
+ err = ubifs_jnl_update (c , dir , & nm , inode , 1 , 0 , 1 );
483
505
if (err )
484
506
goto out_cancel ;
485
507
unlock_2_inodes (dir , inode );
@@ -492,7 +514,6 @@ static int ubifs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
492
514
out_cancel :
493
515
unlock_2_inodes (dir , inode );
494
516
out_inode :
495
- make_bad_inode (inode );
496
517
if (!instantiated )
497
518
iput (inode );
498
519
out_budg :
@@ -1011,6 +1032,7 @@ static int ubifs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1011
1032
if (err )
1012
1033
goto out_inode ;
1013
1034
1035
+ set_nlink (inode , 1 );
1014
1036
mutex_lock (& dir_ui -> ui_mutex );
1015
1037
insert_inode_hash (inode );
1016
1038
inc_nlink (inode );
@@ -1019,7 +1041,7 @@ static int ubifs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1019
1041
dir_ui -> ui_size = dir -> i_size ;
1020
1042
inode_set_mtime_to_ts (dir ,
1021
1043
inode_set_ctime_to_ts (dir , inode_get_ctime (inode )));
1022
- err = ubifs_jnl_update (c , dir , & nm , inode , 0 , 0 , 0 );
1044
+ err = ubifs_jnl_update (c , dir , & nm , inode , 0 , 0 , 1 );
1023
1045
if (err ) {
1024
1046
ubifs_err (c , "cannot create directory, error %d" , err );
1025
1047
goto out_cancel ;
@@ -1036,8 +1058,8 @@ static int ubifs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1036
1058
dir_ui -> ui_size = dir -> i_size ;
1037
1059
drop_nlink (dir );
1038
1060
mutex_unlock (& dir_ui -> ui_mutex );
1061
+ set_nlink (inode , 0 );
1039
1062
out_inode :
1040
- make_bad_inode (inode );
1041
1063
iput (inode );
1042
1064
out_fname :
1043
1065
fscrypt_free_filename (& nm );
@@ -1107,13 +1129,14 @@ static int ubifs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1107
1129
ui = ubifs_inode (inode );
1108
1130
ui -> data = dev ;
1109
1131
ui -> data_len = devlen ;
1132
+ set_nlink (inode , 1 );
1110
1133
1111
1134
mutex_lock (& dir_ui -> ui_mutex );
1112
1135
dir -> i_size += sz_change ;
1113
1136
dir_ui -> ui_size = dir -> i_size ;
1114
1137
inode_set_mtime_to_ts (dir ,
1115
1138
inode_set_ctime_to_ts (dir , inode_get_ctime (inode )));
1116
- err = ubifs_jnl_update (c , dir , & nm , inode , 0 , 0 , 0 );
1139
+ err = ubifs_jnl_update (c , dir , & nm , inode , 0 , 0 , 1 );
1117
1140
if (err )
1118
1141
goto out_cancel ;
1119
1142
mutex_unlock (& dir_ui -> ui_mutex );
@@ -1128,8 +1151,8 @@ static int ubifs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1128
1151
dir -> i_size -= sz_change ;
1129
1152
dir_ui -> ui_size = dir -> i_size ;
1130
1153
mutex_unlock (& dir_ui -> ui_mutex );
1154
+ set_nlink (inode , 0 );
1131
1155
out_inode :
1132
- make_bad_inode (inode );
1133
1156
iput (inode );
1134
1157
out_fname :
1135
1158
fscrypt_free_filename (& nm );
@@ -1208,13 +1231,14 @@ static int ubifs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1208
1231
*/
1209
1232
ui -> data_len = disk_link .len - 1 ;
1210
1233
inode -> i_size = ubifs_inode (inode )-> ui_size = disk_link .len - 1 ;
1234
+ set_nlink (inode , 1 );
1211
1235
1212
1236
mutex_lock (& dir_ui -> ui_mutex );
1213
1237
dir -> i_size += sz_change ;
1214
1238
dir_ui -> ui_size = dir -> i_size ;
1215
1239
inode_set_mtime_to_ts (dir ,
1216
1240
inode_set_ctime_to_ts (dir , inode_get_ctime (inode )));
1217
- err = ubifs_jnl_update (c , dir , & nm , inode , 0 , 0 , 0 );
1241
+ err = ubifs_jnl_update (c , dir , & nm , inode , 0 , 0 , 1 );
1218
1242
if (err )
1219
1243
goto out_cancel ;
1220
1244
mutex_unlock (& dir_ui -> ui_mutex );
@@ -1228,10 +1252,10 @@ static int ubifs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1228
1252
dir -> i_size -= sz_change ;
1229
1253
dir_ui -> ui_size = dir -> i_size ;
1230
1254
mutex_unlock (& dir_ui -> ui_mutex );
1255
+ set_nlink (inode , 0 );
1231
1256
out_inode :
1232
1257
/* Free inode->i_link before inode is marked as bad. */
1233
1258
fscrypt_free_inode (inode );
1234
- make_bad_inode (inode );
1235
1259
iput (inode );
1236
1260
out_fname :
1237
1261
fscrypt_free_filename (& nm );
@@ -1399,14 +1423,10 @@ static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1399
1423
*/
1400
1424
err = ubifs_budget_space (c , & wht_req );
1401
1425
if (err ) {
1402
- /*
1403
- * Whiteout inode can not be written on flash by
1404
- * ubifs_jnl_write_inode(), because it's neither
1405
- * dirty nor zero-nlink.
1406
- */
1407
1426
iput (whiteout );
1408
1427
goto out_release ;
1409
1428
}
1429
+ set_nlink (whiteout , 1 );
1410
1430
1411
1431
/* Add the old_dentry size to the old_dir size. */
1412
1432
old_sz -= CALC_DENT_SIZE (fname_len (& old_nm ));
@@ -1485,7 +1505,7 @@ static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1485
1505
}
1486
1506
1487
1507
err = ubifs_jnl_rename (c , old_dir , old_inode , & old_nm , new_dir ,
1488
- new_inode , & new_nm , whiteout , sync );
1508
+ new_inode , & new_nm , whiteout , sync , !! whiteout );
1489
1509
if (err )
1490
1510
goto out_cancel ;
1491
1511
@@ -1538,6 +1558,7 @@ static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1538
1558
unlock_4_inodes (old_dir , new_dir , new_inode , whiteout );
1539
1559
if (whiteout ) {
1540
1560
ubifs_release_budget (c , & wht_req );
1561
+ set_nlink (whiteout , 0 );
1541
1562
iput (whiteout );
1542
1563
}
1543
1564
out_release :
0 commit comments