0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 #include "ubifs.h"
0050
0051
0052
0053
0054
0055 static inline void zero_ino_node_unused(struct ubifs_ino_node *ino)
0056 {
0057 memset(ino->padding1, 0, 4);
0058 memset(ino->padding2, 0, 26);
0059 }
0060
0061
0062
0063
0064
0065
0066 static inline void zero_dent_node_unused(struct ubifs_dent_node *dent)
0067 {
0068 dent->padding1 = 0;
0069 }
0070
0071
0072
0073
0074
0075
0076 static inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
0077 {
0078 memset(trun->padding, 0, 12);
0079 }
0080
0081 static void ubifs_add_auth_dirt(struct ubifs_info *c, int lnum)
0082 {
0083 if (ubifs_authenticated(c))
0084 ubifs_add_dirt(c, lnum, ubifs_auth_node_sz(c));
0085 }
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 static int reserve_space(struct ubifs_info *c, int jhead, int len)
0099 {
0100 int err = 0, err1, retries = 0, avail, lnum, offs, squeeze;
0101 struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
0102
0103
0104
0105
0106
0107
0108 ubifs_assert(c, !c->ro_media && !c->ro_mount);
0109 squeeze = (jhead == BASEHD);
0110 again:
0111 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
0112
0113 if (c->ro_error) {
0114 err = -EROFS;
0115 goto out_unlock;
0116 }
0117
0118 avail = c->leb_size - wbuf->offs - wbuf->used;
0119 if (wbuf->lnum != -1 && avail >= len)
0120 return 0;
0121
0122
0123
0124
0125
0126 lnum = ubifs_find_free_space(c, len, &offs, squeeze);
0127 if (lnum >= 0)
0128 goto out;
0129
0130 err = lnum;
0131 if (err != -ENOSPC)
0132 goto out_unlock;
0133
0134
0135
0136
0137
0138
0139 dbg_jnl("no free space in jhead %s, run GC", dbg_jhead(jhead));
0140 mutex_unlock(&wbuf->io_mutex);
0141
0142 lnum = ubifs_garbage_collect(c, 0);
0143 if (lnum < 0) {
0144 err = lnum;
0145 if (err != -ENOSPC)
0146 return err;
0147
0148
0149
0150
0151
0152
0153
0154 dbg_jnl("GC couldn't make a free LEB for jhead %s",
0155 dbg_jhead(jhead));
0156 if (retries++ < 2) {
0157 dbg_jnl("retry (%d)", retries);
0158 goto again;
0159 }
0160
0161 dbg_jnl("return -ENOSPC");
0162 return err;
0163 }
0164
0165 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
0166 dbg_jnl("got LEB %d for jhead %s", lnum, dbg_jhead(jhead));
0167 avail = c->leb_size - wbuf->offs - wbuf->used;
0168
0169 if (wbuf->lnum != -1 && avail >= len) {
0170
0171
0172
0173
0174
0175 dbg_jnl("return LEB %d back, already have LEB %d:%d",
0176 lnum, wbuf->lnum, wbuf->offs + wbuf->used);
0177 err = ubifs_return_leb(c, lnum);
0178 if (err)
0179 goto out_unlock;
0180 return 0;
0181 }
0182
0183 offs = 0;
0184
0185 out:
0186
0187
0188
0189
0190
0191
0192
0193
0194 err = ubifs_wbuf_sync_nolock(wbuf);
0195 if (err)
0196 goto out_return;
0197 err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
0198 if (err)
0199 goto out_return;
0200 err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs);
0201 if (err)
0202 goto out_unlock;
0203
0204 return 0;
0205
0206 out_unlock:
0207 mutex_unlock(&wbuf->io_mutex);
0208 return err;
0209
0210 out_return:
0211
0212 ubifs_assert(c, err < 0);
0213 err1 = ubifs_return_leb(c, lnum);
0214 if (err1 && err == -EAGAIN)
0215
0216
0217
0218
0219
0220 err = err1;
0221 mutex_unlock(&wbuf->io_mutex);
0222 return err;
0223 }
0224
0225 static int ubifs_hash_nodes(struct ubifs_info *c, void *node,
0226 int len, struct shash_desc *hash)
0227 {
0228 int auth_node_size = ubifs_auth_node_sz(c);
0229 int err;
0230
0231 while (1) {
0232 const struct ubifs_ch *ch = node;
0233 int nodelen = le32_to_cpu(ch->len);
0234
0235 ubifs_assert(c, len >= auth_node_size);
0236
0237 if (len == auth_node_size)
0238 break;
0239
0240 ubifs_assert(c, len > nodelen);
0241 ubifs_assert(c, ch->magic == cpu_to_le32(UBIFS_NODE_MAGIC));
0242
0243 err = ubifs_shash_update(c, hash, (void *)node, nodelen);
0244 if (err)
0245 return err;
0246
0247 node += ALIGN(nodelen, 8);
0248 len -= ALIGN(nodelen, 8);
0249 }
0250
0251 return ubifs_prepare_auth_node(c, node, hash);
0252 }
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268 static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
0269 int *lnum, int *offs, int sync)
0270 {
0271 int err;
0272 struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
0273
0274 ubifs_assert(c, jhead != GCHD);
0275
0276 *lnum = c->jheads[jhead].wbuf.lnum;
0277 *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
0278 dbg_jnl("jhead %s, LEB %d:%d, len %d",
0279 dbg_jhead(jhead), *lnum, *offs, len);
0280
0281 if (ubifs_authenticated(c)) {
0282 err = ubifs_hash_nodes(c, buf, len, c->jheads[jhead].log_hash);
0283 if (err)
0284 return err;
0285 }
0286
0287 err = ubifs_wbuf_write_nolock(wbuf, buf, len);
0288 if (err)
0289 return err;
0290 if (sync)
0291 err = ubifs_wbuf_sync_nolock(wbuf);
0292 return err;
0293 }
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311 static int make_reservation(struct ubifs_info *c, int jhead, int len)
0312 {
0313 int err, cmt_retries = 0, nospc_retries = 0;
0314
0315 again:
0316 down_read(&c->commit_sem);
0317 err = reserve_space(c, jhead, len);
0318 if (!err)
0319
0320 return 0;
0321 up_read(&c->commit_sem);
0322
0323 if (err == -ENOSPC) {
0324
0325
0326
0327
0328
0329
0330 if (nospc_retries++ < 2) {
0331 dbg_jnl("no space, retry");
0332 err = -EAGAIN;
0333 }
0334
0335
0336
0337
0338
0339
0340
0341 }
0342
0343 if (err != -EAGAIN)
0344 goto out;
0345
0346
0347
0348
0349
0350 if (cmt_retries > 128) {
0351
0352
0353
0354
0355 ubifs_err(c, "stuck in space allocation");
0356 err = -ENOSPC;
0357 goto out;
0358 } else if (cmt_retries > 32)
0359 ubifs_warn(c, "too many space allocation re-tries (%d)",
0360 cmt_retries);
0361
0362 dbg_jnl("-EAGAIN, commit and retry (retried %d times)",
0363 cmt_retries);
0364 cmt_retries += 1;
0365
0366 err = ubifs_run_commit(c);
0367 if (err)
0368 return err;
0369 goto again;
0370
0371 out:
0372 ubifs_err(c, "cannot reserve %d bytes in jhead %d, error %d",
0373 len, jhead, err);
0374 if (err == -ENOSPC) {
0375
0376 down_write(&c->commit_sem);
0377 dump_stack();
0378 ubifs_dump_budg(c, &c->bi);
0379 ubifs_dump_lprops(c);
0380 cmt_retries = dbg_check_lprops(c);
0381 up_write(&c->commit_sem);
0382 }
0383 return err;
0384 }
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395 static inline void release_head(struct ubifs_info *c, int jhead)
0396 {
0397 mutex_unlock(&c->jheads[jhead].wbuf.io_mutex);
0398 }
0399
0400
0401
0402
0403
0404
0405
0406
0407 static void finish_reservation(struct ubifs_info *c)
0408 {
0409 up_read(&c->commit_sem);
0410 }
0411
0412
0413
0414
0415
0416 static int get_dent_type(int mode)
0417 {
0418 switch (mode & S_IFMT) {
0419 case S_IFREG:
0420 return UBIFS_ITYPE_REG;
0421 case S_IFDIR:
0422 return UBIFS_ITYPE_DIR;
0423 case S_IFLNK:
0424 return UBIFS_ITYPE_LNK;
0425 case S_IFBLK:
0426 return UBIFS_ITYPE_BLK;
0427 case S_IFCHR:
0428 return UBIFS_ITYPE_CHR;
0429 case S_IFIFO:
0430 return UBIFS_ITYPE_FIFO;
0431 case S_IFSOCK:
0432 return UBIFS_ITYPE_SOCK;
0433 default:
0434 BUG();
0435 }
0436 return 0;
0437 }
0438
0439
0440
0441
0442
0443
0444
0445
0446 static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
0447 const struct inode *inode, int last)
0448 {
0449 int data_len = 0, last_reference = !inode->i_nlink;
0450 struct ubifs_inode *ui = ubifs_inode(inode);
0451
0452 ino->ch.node_type = UBIFS_INO_NODE;
0453 ino_key_init_flash(c, &ino->key, inode->i_ino);
0454 ino->creat_sqnum = cpu_to_le64(ui->creat_sqnum);
0455 ino->atime_sec = cpu_to_le64(inode->i_atime.tv_sec);
0456 ino->atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
0457 ino->ctime_sec = cpu_to_le64(inode->i_ctime.tv_sec);
0458 ino->ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
0459 ino->mtime_sec = cpu_to_le64(inode->i_mtime.tv_sec);
0460 ino->mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
0461 ino->uid = cpu_to_le32(i_uid_read(inode));
0462 ino->gid = cpu_to_le32(i_gid_read(inode));
0463 ino->mode = cpu_to_le32(inode->i_mode);
0464 ino->flags = cpu_to_le32(ui->flags);
0465 ino->size = cpu_to_le64(ui->ui_size);
0466 ino->nlink = cpu_to_le32(inode->i_nlink);
0467 ino->compr_type = cpu_to_le16(ui->compr_type);
0468 ino->data_len = cpu_to_le32(ui->data_len);
0469 ino->xattr_cnt = cpu_to_le32(ui->xattr_cnt);
0470 ino->xattr_size = cpu_to_le32(ui->xattr_size);
0471 ino->xattr_names = cpu_to_le32(ui->xattr_names);
0472 zero_ino_node_unused(ino);
0473
0474
0475
0476
0477
0478 if (!last_reference) {
0479 memcpy(ino->data, ui->data, ui->data_len);
0480 data_len = ui->data_len;
0481 }
0482
0483 ubifs_prep_grp_node(c, ino, UBIFS_INO_NODE_SZ + data_len, last);
0484 }
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496 static void mark_inode_clean(struct ubifs_info *c, struct ubifs_inode *ui)
0497 {
0498 if (ui->dirty)
0499 ubifs_release_dirty_inode_budget(c, ui);
0500 ui->dirty = 0;
0501 }
0502
0503 static void set_dent_cookie(struct ubifs_info *c, struct ubifs_dent_node *dent)
0504 {
0505 if (c->double_hash)
0506 dent->cookie = (__force __le32) prandom_u32();
0507 else
0508 dent->cookie = 0;
0509 }
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538 int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
0539 const struct fscrypt_name *nm, const struct inode *inode,
0540 int deletion, int xent)
0541 {
0542 int err, dlen, ilen, len, lnum, ino_offs, dent_offs, orphan_added = 0;
0543 int aligned_dlen, aligned_ilen, sync = IS_DIRSYNC(dir);
0544 int last_reference = !!(deletion && inode->i_nlink == 0);
0545 struct ubifs_inode *ui = ubifs_inode(inode);
0546 struct ubifs_inode *host_ui = ubifs_inode(dir);
0547 struct ubifs_dent_node *dent;
0548 struct ubifs_ino_node *ino;
0549 union ubifs_key dent_key, ino_key;
0550 u8 hash_dent[UBIFS_HASH_ARR_SZ];
0551 u8 hash_ino[UBIFS_HASH_ARR_SZ];
0552 u8 hash_ino_host[UBIFS_HASH_ARR_SZ];
0553
0554 ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
0555
0556 dlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
0557 ilen = UBIFS_INO_NODE_SZ;
0558
0559
0560
0561
0562
0563
0564
0565 if (!last_reference) {
0566 ilen += ui->data_len;
0567 sync |= IS_SYNC(inode);
0568 }
0569
0570 aligned_dlen = ALIGN(dlen, 8);
0571 aligned_ilen = ALIGN(ilen, 8);
0572
0573 len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
0574
0575 if (ubifs_authenticated(c))
0576 len += ALIGN(host_ui->data_len, 8) + ubifs_auth_node_sz(c);
0577 else
0578 len += host_ui->data_len;
0579
0580 dent = kzalloc(len, GFP_NOFS);
0581 if (!dent)
0582 return -ENOMEM;
0583
0584
0585 err = make_reservation(c, BASEHD, len);
0586 if (err)
0587 goto out_free;
0588
0589 if (!xent) {
0590 dent->ch.node_type = UBIFS_DENT_NODE;
0591 if (fname_name(nm) == NULL)
0592 dent_key_init_hash(c, &dent_key, dir->i_ino, nm->hash);
0593 else
0594 dent_key_init(c, &dent_key, dir->i_ino, nm);
0595 } else {
0596 dent->ch.node_type = UBIFS_XENT_NODE;
0597 xent_key_init(c, &dent_key, dir->i_ino, nm);
0598 }
0599
0600 key_write(c, &dent_key, dent->key);
0601 dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
0602 dent->type = get_dent_type(inode->i_mode);
0603 dent->nlen = cpu_to_le16(fname_len(nm));
0604 memcpy(dent->name, fname_name(nm), fname_len(nm));
0605 dent->name[fname_len(nm)] = '\0';
0606 set_dent_cookie(c, dent);
0607
0608 zero_dent_node_unused(dent);
0609 ubifs_prep_grp_node(c, dent, dlen, 0);
0610 err = ubifs_node_calc_hash(c, dent, hash_dent);
0611 if (err)
0612 goto out_release;
0613
0614 ino = (void *)dent + aligned_dlen;
0615 pack_inode(c, ino, inode, 0);
0616 err = ubifs_node_calc_hash(c, ino, hash_ino);
0617 if (err)
0618 goto out_release;
0619
0620 ino = (void *)ino + aligned_ilen;
0621 pack_inode(c, ino, dir, 1);
0622 err = ubifs_node_calc_hash(c, ino, hash_ino_host);
0623 if (err)
0624 goto out_release;
0625
0626 if (last_reference) {
0627 err = ubifs_add_orphan(c, inode->i_ino);
0628 if (err) {
0629 release_head(c, BASEHD);
0630 goto out_finish;
0631 }
0632 ui->del_cmtno = c->cmt_no;
0633 orphan_added = 1;
0634 }
0635
0636 err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
0637 if (err)
0638 goto out_release;
0639 if (!sync) {
0640 struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
0641
0642 ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
0643 ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
0644 }
0645 release_head(c, BASEHD);
0646 kfree(dent);
0647 ubifs_add_auth_dirt(c, lnum);
0648
0649 if (deletion) {
0650 if (fname_name(nm) == NULL)
0651 err = ubifs_tnc_remove_dh(c, &dent_key, nm->minor_hash);
0652 else
0653 err = ubifs_tnc_remove_nm(c, &dent_key, nm);
0654 if (err)
0655 goto out_ro;
0656 err = ubifs_add_dirt(c, lnum, dlen);
0657 } else
0658 err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen,
0659 hash_dent, nm);
0660 if (err)
0661 goto out_ro;
0662
0663
0664
0665
0666
0667
0668
0669 ino_key_init(c, &ino_key, inode->i_ino);
0670 ino_offs = dent_offs + aligned_dlen;
0671 err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen, hash_ino);
0672 if (err)
0673 goto out_ro;
0674
0675 ino_key_init(c, &ino_key, dir->i_ino);
0676 ino_offs += aligned_ilen;
0677 err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs,
0678 UBIFS_INO_NODE_SZ + host_ui->data_len, hash_ino_host);
0679 if (err)
0680 goto out_ro;
0681
0682 finish_reservation(c);
0683 spin_lock(&ui->ui_lock);
0684 ui->synced_i_size = ui->ui_size;
0685 spin_unlock(&ui->ui_lock);
0686 if (xent) {
0687 spin_lock(&host_ui->ui_lock);
0688 host_ui->synced_i_size = host_ui->ui_size;
0689 spin_unlock(&host_ui->ui_lock);
0690 }
0691 mark_inode_clean(c, ui);
0692 mark_inode_clean(c, host_ui);
0693 return 0;
0694
0695 out_finish:
0696 finish_reservation(c);
0697 out_free:
0698 kfree(dent);
0699 return err;
0700
0701 out_release:
0702 release_head(c, BASEHD);
0703 kfree(dent);
0704 out_ro:
0705 ubifs_ro_mode(c, err);
0706 if (orphan_added)
0707 ubifs_delete_orphan(c, inode->i_ino);
0708 finish_reservation(c);
0709 return err;
0710 }
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723 int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
0724 const union ubifs_key *key, const void *buf, int len)
0725 {
0726 struct ubifs_data_node *data;
0727 int err, lnum, offs, compr_type, out_len, compr_len, auth_len;
0728 int dlen = COMPRESSED_DATA_NODE_BUF_SZ, allocated = 1;
0729 int write_len;
0730 struct ubifs_inode *ui = ubifs_inode(inode);
0731 bool encrypted = IS_ENCRYPTED(inode);
0732 u8 hash[UBIFS_HASH_ARR_SZ];
0733
0734 dbg_jnlk(key, "ino %lu, blk %u, len %d, key ",
0735 (unsigned long)key_inum(c, key), key_block(c, key), len);
0736 ubifs_assert(c, len <= UBIFS_BLOCK_SIZE);
0737
0738 if (encrypted)
0739 dlen += UBIFS_CIPHER_BLOCK_SIZE;
0740
0741 auth_len = ubifs_auth_node_sz(c);
0742
0743 data = kmalloc(dlen + auth_len, GFP_NOFS | __GFP_NOWARN);
0744 if (!data) {
0745
0746
0747
0748
0749
0750
0751
0752 allocated = 0;
0753 mutex_lock(&c->write_reserve_mutex);
0754 data = c->write_reserve_buf;
0755 }
0756
0757 data->ch.node_type = UBIFS_DATA_NODE;
0758 key_write(c, key, &data->key);
0759 data->size = cpu_to_le32(len);
0760
0761 if (!(ui->flags & UBIFS_COMPR_FL))
0762
0763 compr_type = UBIFS_COMPR_NONE;
0764 else
0765 compr_type = ui->compr_type;
0766
0767 out_len = compr_len = dlen - UBIFS_DATA_NODE_SZ;
0768 ubifs_compress(c, buf, len, &data->data, &compr_len, &compr_type);
0769 ubifs_assert(c, compr_len <= UBIFS_BLOCK_SIZE);
0770
0771 if (encrypted) {
0772 err = ubifs_encrypt(inode, data, compr_len, &out_len, key_block(c, key));
0773 if (err)
0774 goto out_free;
0775
0776 } else {
0777 data->compr_size = 0;
0778 out_len = compr_len;
0779 }
0780
0781 dlen = UBIFS_DATA_NODE_SZ + out_len;
0782 if (ubifs_authenticated(c))
0783 write_len = ALIGN(dlen, 8) + auth_len;
0784 else
0785 write_len = dlen;
0786
0787 data->compr_type = cpu_to_le16(compr_type);
0788
0789
0790 err = make_reservation(c, DATAHD, write_len);
0791 if (err)
0792 goto out_free;
0793
0794 ubifs_prepare_node(c, data, dlen, 0);
0795 err = write_head(c, DATAHD, data, write_len, &lnum, &offs, 0);
0796 if (err)
0797 goto out_release;
0798
0799 err = ubifs_node_calc_hash(c, data, hash);
0800 if (err)
0801 goto out_release;
0802
0803 ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
0804 release_head(c, DATAHD);
0805
0806 ubifs_add_auth_dirt(c, lnum);
0807
0808 err = ubifs_tnc_add(c, key, lnum, offs, dlen, hash);
0809 if (err)
0810 goto out_ro;
0811
0812 finish_reservation(c);
0813 if (!allocated)
0814 mutex_unlock(&c->write_reserve_mutex);
0815 else
0816 kfree(data);
0817 return 0;
0818
0819 out_release:
0820 release_head(c, DATAHD);
0821 out_ro:
0822 ubifs_ro_mode(c, err);
0823 finish_reservation(c);
0824 out_free:
0825 if (!allocated)
0826 mutex_unlock(&c->write_reserve_mutex);
0827 else
0828 kfree(data);
0829 return err;
0830 }
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840
0841 int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
0842 {
0843 int err, lnum, offs;
0844 struct ubifs_ino_node *ino, *ino_start;
0845 struct ubifs_inode *ui = ubifs_inode(inode);
0846 int sync = 0, write_len = 0, ilen = UBIFS_INO_NODE_SZ;
0847 int last_reference = !inode->i_nlink;
0848 int kill_xattrs = ui->xattr_cnt && last_reference;
0849 u8 hash[UBIFS_HASH_ARR_SZ];
0850
0851 dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
0852
0853
0854
0855
0856
0857 if (!last_reference) {
0858 ilen += ui->data_len;
0859 sync = IS_SYNC(inode);
0860 } else if (kill_xattrs) {
0861 write_len += UBIFS_INO_NODE_SZ * ui->xattr_cnt;
0862 }
0863
0864 if (ubifs_authenticated(c))
0865 write_len += ALIGN(ilen, 8) + ubifs_auth_node_sz(c);
0866 else
0867 write_len += ilen;
0868
0869 ino_start = ino = kmalloc(write_len, GFP_NOFS);
0870 if (!ino)
0871 return -ENOMEM;
0872
0873
0874 err = make_reservation(c, BASEHD, write_len);
0875 if (err)
0876 goto out_free;
0877
0878 if (kill_xattrs) {
0879 union ubifs_key key;
0880 struct fscrypt_name nm = {0};
0881 struct inode *xino;
0882 struct ubifs_dent_node *xent, *pxent = NULL;
0883
0884 if (ui->xattr_cnt > ubifs_xattr_max_cnt(c)) {
0885 err = -EPERM;
0886 ubifs_err(c, "Cannot delete inode, it has too much xattrs!");
0887 goto out_release;
0888 }
0889
0890 lowest_xent_key(c, &key, inode->i_ino);
0891 while (1) {
0892 xent = ubifs_tnc_next_ent(c, &key, &nm);
0893 if (IS_ERR(xent)) {
0894 err = PTR_ERR(xent);
0895 if (err == -ENOENT)
0896 break;
0897
0898 kfree(pxent);
0899 goto out_release;
0900 }
0901
0902 fname_name(&nm) = xent->name;
0903 fname_len(&nm) = le16_to_cpu(xent->nlen);
0904
0905 xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
0906 if (IS_ERR(xino)) {
0907 err = PTR_ERR(xino);
0908 ubifs_err(c, "dead directory entry '%s', error %d",
0909 xent->name, err);
0910 ubifs_ro_mode(c, err);
0911 kfree(pxent);
0912 kfree(xent);
0913 goto out_release;
0914 }
0915 ubifs_assert(c, ubifs_inode(xino)->xattr);
0916
0917 clear_nlink(xino);
0918 pack_inode(c, ino, xino, 0);
0919 ino = (void *)ino + UBIFS_INO_NODE_SZ;
0920 iput(xino);
0921
0922 kfree(pxent);
0923 pxent = xent;
0924 key_read(c, &xent->key, &key);
0925 }
0926 kfree(pxent);
0927 }
0928
0929 pack_inode(c, ino, inode, 1);
0930 err = ubifs_node_calc_hash(c, ino, hash);
0931 if (err)
0932 goto out_release;
0933
0934 err = write_head(c, BASEHD, ino_start, write_len, &lnum, &offs, sync);
0935 if (err)
0936 goto out_release;
0937 if (!sync)
0938 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
0939 inode->i_ino);
0940 release_head(c, BASEHD);
0941
0942 if (last_reference) {
0943 err = ubifs_tnc_remove_ino(c, inode->i_ino);
0944 if (err)
0945 goto out_ro;
0946 ubifs_delete_orphan(c, inode->i_ino);
0947 err = ubifs_add_dirt(c, lnum, write_len);
0948 } else {
0949 union ubifs_key key;
0950
0951 ubifs_add_auth_dirt(c, lnum);
0952
0953 ino_key_init(c, &key, inode->i_ino);
0954 err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash);
0955 }
0956 if (err)
0957 goto out_ro;
0958
0959 finish_reservation(c);
0960 spin_lock(&ui->ui_lock);
0961 ui->synced_i_size = ui->ui_size;
0962 spin_unlock(&ui->ui_lock);
0963 kfree(ino_start);
0964 return 0;
0965
0966 out_release:
0967 release_head(c, BASEHD);
0968 out_ro:
0969 ubifs_ro_mode(c, err);
0970 finish_reservation(c);
0971 out_free:
0972 kfree(ino_start);
0973 return err;
0974 }
0975
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993
0994
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005 int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
1006 {
1007 int err;
1008 struct ubifs_inode *ui = ubifs_inode(inode);
1009
1010 ubifs_assert(c, inode->i_nlink == 0);
1011
1012 if (ui->xattr_cnt || ui->del_cmtno != c->cmt_no)
1013
1014 return ubifs_jnl_write_inode(c, inode);
1015
1016 down_read(&c->commit_sem);
1017
1018
1019
1020
1021 if (ui->del_cmtno != c->cmt_no) {
1022 up_read(&c->commit_sem);
1023 return ubifs_jnl_write_inode(c, inode);
1024 }
1025
1026 err = ubifs_tnc_remove_ino(c, inode->i_ino);
1027 if (err)
1028 ubifs_ro_mode(c, err);
1029 else
1030 ubifs_delete_orphan(c, inode->i_ino);
1031 up_read(&c->commit_sem);
1032 return err;
1033 }
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051 int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
1052 const struct inode *fst_inode,
1053 const struct fscrypt_name *fst_nm,
1054 const struct inode *snd_dir,
1055 const struct inode *snd_inode,
1056 const struct fscrypt_name *snd_nm, int sync)
1057 {
1058 union ubifs_key key;
1059 struct ubifs_dent_node *dent1, *dent2;
1060 int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
1061 int aligned_dlen1, aligned_dlen2;
1062 int twoparents = (fst_dir != snd_dir);
1063 void *p;
1064 u8 hash_dent1[UBIFS_HASH_ARR_SZ];
1065 u8 hash_dent2[UBIFS_HASH_ARR_SZ];
1066 u8 hash_p1[UBIFS_HASH_ARR_SZ];
1067 u8 hash_p2[UBIFS_HASH_ARR_SZ];
1068
1069 ubifs_assert(c, ubifs_inode(fst_dir)->data_len == 0);
1070 ubifs_assert(c, ubifs_inode(snd_dir)->data_len == 0);
1071 ubifs_assert(c, mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
1072 ubifs_assert(c, mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
1073
1074 dlen1 = UBIFS_DENT_NODE_SZ + fname_len(snd_nm) + 1;
1075 dlen2 = UBIFS_DENT_NODE_SZ + fname_len(fst_nm) + 1;
1076 aligned_dlen1 = ALIGN(dlen1, 8);
1077 aligned_dlen2 = ALIGN(dlen2, 8);
1078
1079 len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
1080 if (twoparents)
1081 len += plen;
1082
1083 len += ubifs_auth_node_sz(c);
1084
1085 dent1 = kzalloc(len, GFP_NOFS);
1086 if (!dent1)
1087 return -ENOMEM;
1088
1089
1090 err = make_reservation(c, BASEHD, len);
1091 if (err)
1092 goto out_free;
1093
1094
1095 dent1->ch.node_type = UBIFS_DENT_NODE;
1096 dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, snd_nm);
1097 dent1->inum = cpu_to_le64(fst_inode->i_ino);
1098 dent1->type = get_dent_type(fst_inode->i_mode);
1099 dent1->nlen = cpu_to_le16(fname_len(snd_nm));
1100 memcpy(dent1->name, fname_name(snd_nm), fname_len(snd_nm));
1101 dent1->name[fname_len(snd_nm)] = '\0';
1102 set_dent_cookie(c, dent1);
1103 zero_dent_node_unused(dent1);
1104 ubifs_prep_grp_node(c, dent1, dlen1, 0);
1105 err = ubifs_node_calc_hash(c, dent1, hash_dent1);
1106 if (err)
1107 goto out_release;
1108
1109
1110 dent2 = (void *)dent1 + aligned_dlen1;
1111 dent2->ch.node_type = UBIFS_DENT_NODE;
1112 dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, fst_nm);
1113 dent2->inum = cpu_to_le64(snd_inode->i_ino);
1114 dent2->type = get_dent_type(snd_inode->i_mode);
1115 dent2->nlen = cpu_to_le16(fname_len(fst_nm));
1116 memcpy(dent2->name, fname_name(fst_nm), fname_len(fst_nm));
1117 dent2->name[fname_len(fst_nm)] = '\0';
1118 set_dent_cookie(c, dent2);
1119 zero_dent_node_unused(dent2);
1120 ubifs_prep_grp_node(c, dent2, dlen2, 0);
1121 err = ubifs_node_calc_hash(c, dent2, hash_dent2);
1122 if (err)
1123 goto out_release;
1124
1125 p = (void *)dent2 + aligned_dlen2;
1126 if (!twoparents) {
1127 pack_inode(c, p, fst_dir, 1);
1128 err = ubifs_node_calc_hash(c, p, hash_p1);
1129 if (err)
1130 goto out_release;
1131 } else {
1132 pack_inode(c, p, fst_dir, 0);
1133 err = ubifs_node_calc_hash(c, p, hash_p1);
1134 if (err)
1135 goto out_release;
1136 p += ALIGN(plen, 8);
1137 pack_inode(c, p, snd_dir, 1);
1138 err = ubifs_node_calc_hash(c, p, hash_p2);
1139 if (err)
1140 goto out_release;
1141 }
1142
1143 err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
1144 if (err)
1145 goto out_release;
1146 if (!sync) {
1147 struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1148
1149 ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
1150 ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
1151 }
1152 release_head(c, BASEHD);
1153
1154 ubifs_add_auth_dirt(c, lnum);
1155
1156 dent_key_init(c, &key, snd_dir->i_ino, snd_nm);
1157 err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, snd_nm);
1158 if (err)
1159 goto out_ro;
1160
1161 offs += aligned_dlen1;
1162 dent_key_init(c, &key, fst_dir->i_ino, fst_nm);
1163 err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, fst_nm);
1164 if (err)
1165 goto out_ro;
1166
1167 offs += aligned_dlen2;
1168
1169 ino_key_init(c, &key, fst_dir->i_ino);
1170 err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p1);
1171 if (err)
1172 goto out_ro;
1173
1174 if (twoparents) {
1175 offs += ALIGN(plen, 8);
1176 ino_key_init(c, &key, snd_dir->i_ino);
1177 err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p2);
1178 if (err)
1179 goto out_ro;
1180 }
1181
1182 finish_reservation(c);
1183
1184 mark_inode_clean(c, ubifs_inode(fst_dir));
1185 if (twoparents)
1186 mark_inode_clean(c, ubifs_inode(snd_dir));
1187 kfree(dent1);
1188 return 0;
1189
1190 out_release:
1191 release_head(c, BASEHD);
1192 out_ro:
1193 ubifs_ro_mode(c, err);
1194 finish_reservation(c);
1195 out_free:
1196 kfree(dent1);
1197 return err;
1198 }
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214 int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
1215 const struct inode *old_inode,
1216 const struct fscrypt_name *old_nm,
1217 const struct inode *new_dir,
1218 const struct inode *new_inode,
1219 const struct fscrypt_name *new_nm,
1220 const struct inode *whiteout, int sync)
1221 {
1222 void *p;
1223 union ubifs_key key;
1224 struct ubifs_dent_node *dent, *dent2;
1225 int err, dlen1, dlen2, ilen, wlen, lnum, offs, len, orphan_added = 0;
1226 int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
1227 int last_reference = !!(new_inode && new_inode->i_nlink == 0);
1228 int move = (old_dir != new_dir);
1229 struct ubifs_inode *new_ui, *whiteout_ui;
1230 u8 hash_old_dir[UBIFS_HASH_ARR_SZ];
1231 u8 hash_new_dir[UBIFS_HASH_ARR_SZ];
1232 u8 hash_new_inode[UBIFS_HASH_ARR_SZ];
1233 u8 hash_whiteout_inode[UBIFS_HASH_ARR_SZ];
1234 u8 hash_dent1[UBIFS_HASH_ARR_SZ];
1235 u8 hash_dent2[UBIFS_HASH_ARR_SZ];
1236
1237 ubifs_assert(c, ubifs_inode(old_dir)->data_len == 0);
1238 ubifs_assert(c, ubifs_inode(new_dir)->data_len == 0);
1239 ubifs_assert(c, mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
1240 ubifs_assert(c, mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
1241
1242 dlen1 = UBIFS_DENT_NODE_SZ + fname_len(new_nm) + 1;
1243 dlen2 = UBIFS_DENT_NODE_SZ + fname_len(old_nm) + 1;
1244 if (new_inode) {
1245 new_ui = ubifs_inode(new_inode);
1246 ubifs_assert(c, mutex_is_locked(&new_ui->ui_mutex));
1247 ilen = UBIFS_INO_NODE_SZ;
1248 if (!last_reference)
1249 ilen += new_ui->data_len;
1250 } else
1251 ilen = 0;
1252
1253 if (whiteout) {
1254 whiteout_ui = ubifs_inode(whiteout);
1255 ubifs_assert(c, mutex_is_locked(&whiteout_ui->ui_mutex));
1256 ubifs_assert(c, whiteout->i_nlink == 1);
1257 ubifs_assert(c, !whiteout_ui->dirty);
1258 wlen = UBIFS_INO_NODE_SZ;
1259 wlen += whiteout_ui->data_len;
1260 } else
1261 wlen = 0;
1262
1263 aligned_dlen1 = ALIGN(dlen1, 8);
1264 aligned_dlen2 = ALIGN(dlen2, 8);
1265 len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) +
1266 ALIGN(wlen, 8) + ALIGN(plen, 8);
1267 if (move)
1268 len += plen;
1269
1270 len += ubifs_auth_node_sz(c);
1271
1272 dent = kzalloc(len, GFP_NOFS);
1273 if (!dent)
1274 return -ENOMEM;
1275
1276
1277 err = make_reservation(c, BASEHD, len);
1278 if (err)
1279 goto out_free;
1280
1281
1282 dent->ch.node_type = UBIFS_DENT_NODE;
1283 dent_key_init_flash(c, &dent->key, new_dir->i_ino, new_nm);
1284 dent->inum = cpu_to_le64(old_inode->i_ino);
1285 dent->type = get_dent_type(old_inode->i_mode);
1286 dent->nlen = cpu_to_le16(fname_len(new_nm));
1287 memcpy(dent->name, fname_name(new_nm), fname_len(new_nm));
1288 dent->name[fname_len(new_nm)] = '\0';
1289 set_dent_cookie(c, dent);
1290 zero_dent_node_unused(dent);
1291 ubifs_prep_grp_node(c, dent, dlen1, 0);
1292 err = ubifs_node_calc_hash(c, dent, hash_dent1);
1293 if (err)
1294 goto out_release;
1295
1296 dent2 = (void *)dent + aligned_dlen1;
1297 dent2->ch.node_type = UBIFS_DENT_NODE;
1298 dent_key_init_flash(c, &dent2->key, old_dir->i_ino, old_nm);
1299
1300 if (whiteout) {
1301 dent2->inum = cpu_to_le64(whiteout->i_ino);
1302 dent2->type = get_dent_type(whiteout->i_mode);
1303 } else {
1304
1305 dent2->inum = 0;
1306 dent2->type = DT_UNKNOWN;
1307 }
1308 dent2->nlen = cpu_to_le16(fname_len(old_nm));
1309 memcpy(dent2->name, fname_name(old_nm), fname_len(old_nm));
1310 dent2->name[fname_len(old_nm)] = '\0';
1311 set_dent_cookie(c, dent2);
1312 zero_dent_node_unused(dent2);
1313 ubifs_prep_grp_node(c, dent2, dlen2, 0);
1314 err = ubifs_node_calc_hash(c, dent2, hash_dent2);
1315 if (err)
1316 goto out_release;
1317
1318 p = (void *)dent2 + aligned_dlen2;
1319 if (new_inode) {
1320 pack_inode(c, p, new_inode, 0);
1321 err = ubifs_node_calc_hash(c, p, hash_new_inode);
1322 if (err)
1323 goto out_release;
1324
1325 p += ALIGN(ilen, 8);
1326 }
1327
1328 if (whiteout) {
1329 pack_inode(c, p, whiteout, 0);
1330 err = ubifs_node_calc_hash(c, p, hash_whiteout_inode);
1331 if (err)
1332 goto out_release;
1333
1334 p += ALIGN(wlen, 8);
1335 }
1336
1337 if (!move) {
1338 pack_inode(c, p, old_dir, 1);
1339 err = ubifs_node_calc_hash(c, p, hash_old_dir);
1340 if (err)
1341 goto out_release;
1342 } else {
1343 pack_inode(c, p, old_dir, 0);
1344 err = ubifs_node_calc_hash(c, p, hash_old_dir);
1345 if (err)
1346 goto out_release;
1347
1348 p += ALIGN(plen, 8);
1349 pack_inode(c, p, new_dir, 1);
1350 err = ubifs_node_calc_hash(c, p, hash_new_dir);
1351 if (err)
1352 goto out_release;
1353 }
1354
1355 if (last_reference) {
1356 err = ubifs_add_orphan(c, new_inode->i_ino);
1357 if (err) {
1358 release_head(c, BASEHD);
1359 goto out_finish;
1360 }
1361 new_ui->del_cmtno = c->cmt_no;
1362 orphan_added = 1;
1363 }
1364
1365 err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
1366 if (err)
1367 goto out_release;
1368 if (!sync) {
1369 struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1370
1371 ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
1372 ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
1373 if (new_inode)
1374 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
1375 new_inode->i_ino);
1376 if (whiteout)
1377 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
1378 whiteout->i_ino);
1379 }
1380 release_head(c, BASEHD);
1381
1382 ubifs_add_auth_dirt(c, lnum);
1383
1384 dent_key_init(c, &key, new_dir->i_ino, new_nm);
1385 err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, new_nm);
1386 if (err)
1387 goto out_ro;
1388
1389 offs += aligned_dlen1;
1390 if (whiteout) {
1391 dent_key_init(c, &key, old_dir->i_ino, old_nm);
1392 err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, old_nm);
1393 if (err)
1394 goto out_ro;
1395 } else {
1396 err = ubifs_add_dirt(c, lnum, dlen2);
1397 if (err)
1398 goto out_ro;
1399
1400 dent_key_init(c, &key, old_dir->i_ino, old_nm);
1401 err = ubifs_tnc_remove_nm(c, &key, old_nm);
1402 if (err)
1403 goto out_ro;
1404 }
1405
1406 offs += aligned_dlen2;
1407 if (new_inode) {
1408 ino_key_init(c, &key, new_inode->i_ino);
1409 err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash_new_inode);
1410 if (err)
1411 goto out_ro;
1412 offs += ALIGN(ilen, 8);
1413 }
1414
1415 if (whiteout) {
1416 ino_key_init(c, &key, whiteout->i_ino);
1417 err = ubifs_tnc_add(c, &key, lnum, offs, wlen,
1418 hash_whiteout_inode);
1419 if (err)
1420 goto out_ro;
1421 offs += ALIGN(wlen, 8);
1422 }
1423
1424 ino_key_init(c, &key, old_dir->i_ino);
1425 err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_old_dir);
1426 if (err)
1427 goto out_ro;
1428
1429 if (move) {
1430 offs += ALIGN(plen, 8);
1431 ino_key_init(c, &key, new_dir->i_ino);
1432 err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_new_dir);
1433 if (err)
1434 goto out_ro;
1435 }
1436
1437 finish_reservation(c);
1438 if (new_inode) {
1439 mark_inode_clean(c, new_ui);
1440 spin_lock(&new_ui->ui_lock);
1441 new_ui->synced_i_size = new_ui->ui_size;
1442 spin_unlock(&new_ui->ui_lock);
1443 }
1444
1445
1446
1447
1448
1449 mark_inode_clean(c, ubifs_inode(old_dir));
1450 if (move)
1451 mark_inode_clean(c, ubifs_inode(new_dir));
1452 kfree(dent);
1453 return 0;
1454
1455 out_release:
1456 release_head(c, BASEHD);
1457 out_ro:
1458 ubifs_ro_mode(c, err);
1459 if (orphan_added)
1460 ubifs_delete_orphan(c, new_inode->i_ino);
1461 out_finish:
1462 finish_reservation(c);
1463 out_free:
1464 kfree(dent);
1465 return err;
1466 }
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479 static int truncate_data_node(const struct ubifs_info *c, const struct inode *inode,
1480 unsigned int block, struct ubifs_data_node *dn,
1481 int *new_len)
1482 {
1483 void *buf;
1484 int err, dlen, compr_type, out_len, old_dlen;
1485
1486 out_len = le32_to_cpu(dn->size);
1487 buf = kmalloc_array(out_len, WORST_COMPR_FACTOR, GFP_NOFS);
1488 if (!buf)
1489 return -ENOMEM;
1490
1491 dlen = old_dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
1492 compr_type = le16_to_cpu(dn->compr_type);
1493
1494 if (IS_ENCRYPTED(inode)) {
1495 err = ubifs_decrypt(inode, dn, &dlen, block);
1496 if (err)
1497 goto out;
1498 }
1499
1500 if (compr_type == UBIFS_COMPR_NONE) {
1501 out_len = *new_len;
1502 } else {
1503 err = ubifs_decompress(c, &dn->data, dlen, buf, &out_len, compr_type);
1504 if (err)
1505 goto out;
1506
1507 ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
1508 }
1509
1510 if (IS_ENCRYPTED(inode)) {
1511 err = ubifs_encrypt(inode, dn, out_len, &old_dlen, block);
1512 if (err)
1513 goto out;
1514
1515 out_len = old_dlen;
1516 } else {
1517 dn->compr_size = 0;
1518 }
1519
1520 ubifs_assert(c, out_len <= UBIFS_BLOCK_SIZE);
1521 dn->compr_type = cpu_to_le16(compr_type);
1522 dn->size = cpu_to_le32(*new_len);
1523 *new_len = UBIFS_DATA_NODE_SZ + out_len;
1524 err = 0;
1525 out:
1526 kfree(buf);
1527 return err;
1528 }
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545 int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
1546 loff_t old_size, loff_t new_size)
1547 {
1548 union ubifs_key key, to_key;
1549 struct ubifs_ino_node *ino;
1550 struct ubifs_trun_node *trun;
1551 struct ubifs_data_node *dn;
1552 int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
1553 struct ubifs_inode *ui = ubifs_inode(inode);
1554 ino_t inum = inode->i_ino;
1555 unsigned int blk;
1556 u8 hash_ino[UBIFS_HASH_ARR_SZ];
1557 u8 hash_dn[UBIFS_HASH_ARR_SZ];
1558
1559 dbg_jnl("ino %lu, size %lld -> %lld",
1560 (unsigned long)inum, old_size, new_size);
1561 ubifs_assert(c, !ui->data_len);
1562 ubifs_assert(c, S_ISREG(inode->i_mode));
1563 ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
1564
1565 sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
1566 UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
1567
1568 sz += ubifs_auth_node_sz(c);
1569
1570 ino = kmalloc(sz, GFP_NOFS);
1571 if (!ino)
1572 return -ENOMEM;
1573
1574 trun = (void *)ino + UBIFS_INO_NODE_SZ;
1575 trun->ch.node_type = UBIFS_TRUN_NODE;
1576 trun->inum = cpu_to_le32(inum);
1577 trun->old_size = cpu_to_le64(old_size);
1578 trun->new_size = cpu_to_le64(new_size);
1579 zero_trun_node_unused(trun);
1580
1581 dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
1582 if (dlen) {
1583
1584 dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
1585 blk = new_size >> UBIFS_BLOCK_SHIFT;
1586 data_key_init(c, &key, inum, blk);
1587 dbg_jnlk(&key, "last block key ");
1588 err = ubifs_tnc_lookup(c, &key, dn);
1589 if (err == -ENOENT)
1590 dlen = 0;
1591 else if (err)
1592 goto out_free;
1593 else {
1594 int dn_len = le32_to_cpu(dn->size);
1595
1596 if (dn_len <= 0 || dn_len > UBIFS_BLOCK_SIZE) {
1597 ubifs_err(c, "bad data node (block %u, inode %lu)",
1598 blk, inode->i_ino);
1599 ubifs_dump_node(c, dn, sz - UBIFS_INO_NODE_SZ -
1600 UBIFS_TRUN_NODE_SZ);
1601 goto out_free;
1602 }
1603
1604 if (dn_len <= dlen)
1605 dlen = 0;
1606 else {
1607 err = truncate_data_node(c, inode, blk, dn, &dlen);
1608 if (err)
1609 goto out_free;
1610 }
1611 }
1612 }
1613
1614
1615 len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
1616
1617 if (ubifs_authenticated(c))
1618 len += ALIGN(dlen, 8) + ubifs_auth_node_sz(c);
1619 else
1620 len += dlen;
1621
1622 err = make_reservation(c, BASEHD, len);
1623 if (err)
1624 goto out_free;
1625
1626 pack_inode(c, ino, inode, 0);
1627 err = ubifs_node_calc_hash(c, ino, hash_ino);
1628 if (err)
1629 goto out_release;
1630
1631 ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
1632 if (dlen) {
1633 ubifs_prep_grp_node(c, dn, dlen, 1);
1634 err = ubifs_node_calc_hash(c, dn, hash_dn);
1635 if (err)
1636 goto out_release;
1637 }
1638
1639 err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
1640 if (err)
1641 goto out_release;
1642 if (!sync)
1643 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
1644 release_head(c, BASEHD);
1645
1646 ubifs_add_auth_dirt(c, lnum);
1647
1648 if (dlen) {
1649 sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
1650 err = ubifs_tnc_add(c, &key, lnum, sz, dlen, hash_dn);
1651 if (err)
1652 goto out_ro;
1653 }
1654
1655 ino_key_init(c, &key, inum);
1656 err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ, hash_ino);
1657 if (err)
1658 goto out_ro;
1659
1660 err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
1661 if (err)
1662 goto out_ro;
1663
1664 bit = new_size & (UBIFS_BLOCK_SIZE - 1);
1665 blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
1666 data_key_init(c, &key, inum, blk);
1667
1668 bit = old_size & (UBIFS_BLOCK_SIZE - 1);
1669 blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
1670 data_key_init(c, &to_key, inum, blk);
1671
1672 err = ubifs_tnc_remove_range(c, &key, &to_key);
1673 if (err)
1674 goto out_ro;
1675
1676 finish_reservation(c);
1677 spin_lock(&ui->ui_lock);
1678 ui->synced_i_size = ui->ui_size;
1679 spin_unlock(&ui->ui_lock);
1680 mark_inode_clean(c, ui);
1681 kfree(ino);
1682 return 0;
1683
1684 out_release:
1685 release_head(c, BASEHD);
1686 out_ro:
1687 ubifs_ro_mode(c, err);
1688 finish_reservation(c);
1689 out_free:
1690 kfree(ino);
1691 return err;
1692 }
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707 int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
1708 const struct inode *inode,
1709 const struct fscrypt_name *nm)
1710 {
1711 int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen, write_len;
1712 struct ubifs_dent_node *xent;
1713 struct ubifs_ino_node *ino;
1714 union ubifs_key xent_key, key1, key2;
1715 int sync = IS_DIRSYNC(host);
1716 struct ubifs_inode *host_ui = ubifs_inode(host);
1717 u8 hash[UBIFS_HASH_ARR_SZ];
1718
1719 ubifs_assert(c, inode->i_nlink == 0);
1720 ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
1721
1722
1723
1724
1725
1726 xlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
1727 aligned_xlen = ALIGN(xlen, 8);
1728 hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
1729 len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
1730
1731 write_len = len + ubifs_auth_node_sz(c);
1732
1733 xent = kzalloc(write_len, GFP_NOFS);
1734 if (!xent)
1735 return -ENOMEM;
1736
1737
1738 err = make_reservation(c, BASEHD, write_len);
1739 if (err) {
1740 kfree(xent);
1741 return err;
1742 }
1743
1744 xent->ch.node_type = UBIFS_XENT_NODE;
1745 xent_key_init(c, &xent_key, host->i_ino, nm);
1746 key_write(c, &xent_key, xent->key);
1747 xent->inum = 0;
1748 xent->type = get_dent_type(inode->i_mode);
1749 xent->nlen = cpu_to_le16(fname_len(nm));
1750 memcpy(xent->name, fname_name(nm), fname_len(nm));
1751 xent->name[fname_len(nm)] = '\0';
1752 zero_dent_node_unused(xent);
1753 ubifs_prep_grp_node(c, xent, xlen, 0);
1754
1755 ino = (void *)xent + aligned_xlen;
1756 pack_inode(c, ino, inode, 0);
1757 ino = (void *)ino + UBIFS_INO_NODE_SZ;
1758 pack_inode(c, ino, host, 1);
1759 err = ubifs_node_calc_hash(c, ino, hash);
1760 if (err)
1761 goto out_release;
1762
1763 err = write_head(c, BASEHD, xent, write_len, &lnum, &xent_offs, sync);
1764 if (!sync && !err)
1765 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
1766 release_head(c, BASEHD);
1767
1768 ubifs_add_auth_dirt(c, lnum);
1769 kfree(xent);
1770 if (err)
1771 goto out_ro;
1772
1773
1774 err = ubifs_tnc_remove_nm(c, &xent_key, nm);
1775 if (err)
1776 goto out_ro;
1777 err = ubifs_add_dirt(c, lnum, xlen);
1778 if (err)
1779 goto out_ro;
1780
1781
1782
1783
1784
1785 lowest_ino_key(c, &key1, inode->i_ino);
1786 highest_ino_key(c, &key2, inode->i_ino);
1787 err = ubifs_tnc_remove_range(c, &key1, &key2);
1788 if (err)
1789 goto out_ro;
1790 err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
1791 if (err)
1792 goto out_ro;
1793
1794
1795 ino_key_init(c, &key1, host->i_ino);
1796 err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen, hash);
1797 if (err)
1798 goto out_ro;
1799
1800 finish_reservation(c);
1801 spin_lock(&host_ui->ui_lock);
1802 host_ui->synced_i_size = host_ui->ui_size;
1803 spin_unlock(&host_ui->ui_lock);
1804 mark_inode_clean(c, host_ui);
1805 return 0;
1806
1807 out_release:
1808 kfree(xent);
1809 release_head(c, BASEHD);
1810 out_ro:
1811 ubifs_ro_mode(c, err);
1812 finish_reservation(c);
1813 return err;
1814 }
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829 int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
1830 const struct inode *host)
1831 {
1832 int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
1833 struct ubifs_inode *host_ui = ubifs_inode(host);
1834 struct ubifs_ino_node *ino;
1835 union ubifs_key key;
1836 int sync = IS_DIRSYNC(host);
1837 u8 hash_host[UBIFS_HASH_ARR_SZ];
1838 u8 hash[UBIFS_HASH_ARR_SZ];
1839
1840 dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
1841 ubifs_assert(c, inode->i_nlink > 0);
1842 ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
1843
1844 len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
1845 len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
1846 aligned_len1 = ALIGN(len1, 8);
1847 aligned_len = aligned_len1 + ALIGN(len2, 8);
1848
1849 aligned_len += ubifs_auth_node_sz(c);
1850
1851 ino = kzalloc(aligned_len, GFP_NOFS);
1852 if (!ino)
1853 return -ENOMEM;
1854
1855
1856 err = make_reservation(c, BASEHD, aligned_len);
1857 if (err)
1858 goto out_free;
1859
1860 pack_inode(c, ino, host, 0);
1861 err = ubifs_node_calc_hash(c, ino, hash_host);
1862 if (err)
1863 goto out_release;
1864 pack_inode(c, (void *)ino + aligned_len1, inode, 1);
1865 err = ubifs_node_calc_hash(c, (void *)ino + aligned_len1, hash);
1866 if (err)
1867 goto out_release;
1868
1869 err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
1870 if (!sync && !err) {
1871 struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1872
1873 ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
1874 ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
1875 }
1876 release_head(c, BASEHD);
1877 if (err)
1878 goto out_ro;
1879
1880 ubifs_add_auth_dirt(c, lnum);
1881
1882 ino_key_init(c, &key, host->i_ino);
1883 err = ubifs_tnc_add(c, &key, lnum, offs, len1, hash_host);
1884 if (err)
1885 goto out_ro;
1886
1887 ino_key_init(c, &key, inode->i_ino);
1888 err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2, hash);
1889 if (err)
1890 goto out_ro;
1891
1892 finish_reservation(c);
1893 spin_lock(&host_ui->ui_lock);
1894 host_ui->synced_i_size = host_ui->ui_size;
1895 spin_unlock(&host_ui->ui_lock);
1896 mark_inode_clean(c, host_ui);
1897 kfree(ino);
1898 return 0;
1899
1900 out_release:
1901 release_head(c, BASEHD);
1902 out_ro:
1903 ubifs_ro_mode(c, err);
1904 finish_reservation(c);
1905 out_free:
1906 kfree(ino);
1907 return err;
1908 }
1909