Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 2 | /* * This file is part of UBIFS. |
| 3 | * |
| 4 | * Copyright (C) 2006-2008 Nokia Corporation. |
| 5 | * Copyright (C) 2006, 2007 University of Szeged, Hungary |
| 6 | * |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 7 | * Authors: Artem Bityutskiy (Битюцкий Артём) |
| 8 | * Adrian Hunter |
| 9 | * Zoltan Sogor |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * This file implements directory operations. |
| 14 | * |
| 15 | * All FS operations in this file allocate budget before writing anything to the |
| 16 | * media. If they fail to allocate it, the error is returned. The only |
| 17 | * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even |
| 18 | * if they unable to allocate the budget, because deletion %-ENOSPC failure is |
| 19 | * not what users are usually ready to get. UBIFS budgeting subsystem has some |
| 20 | * space reserved for these purposes. |
| 21 | * |
| 22 | * All operations in this file write all inodes which they change straight |
| 23 | * away, instead of marking them dirty. For example, 'ubifs_link()' changes |
| 24 | * @i_size of the parent inode and writes the parent inode together with the |
| 25 | * target inode. This was done to simplify file-system recovery which would |
| 26 | * otherwise be very difficult to do. The only exception is rename which marks |
| 27 | * the re-named inode dirty (because its @i_ctime is updated) but does not |
| 28 | * write it, but just marks it as dirty. |
| 29 | */ |
| 30 | |
| 31 | #include "ubifs.h" |
| 32 | |
| 33 | /** |
| 34 | * inherit_flags - inherit flags of the parent inode. |
| 35 | * @dir: parent inode |
| 36 | * @mode: new inode mode flags |
| 37 | * |
| 38 | * This is a helper function for 'ubifs_new_inode()' which inherits flag of the |
| 39 | * parent directory inode @dir. UBIFS inodes inherit the following flags: |
| 40 | * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on |
| 41 | * sub-directory basis; |
| 42 | * o %UBIFS_SYNC_FL - useful for the same reasons; |
| 43 | * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories. |
| 44 | * |
| 45 | * This function returns the inherited flags. |
| 46 | */ |
Al Viro | ad44be5 | 2011-07-26 03:12:59 -0400 | [diff] [blame] | 47 | static int inherit_flags(const struct inode *dir, umode_t mode) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 48 | { |
| 49 | int flags; |
| 50 | const struct ubifs_inode *ui = ubifs_inode(dir); |
| 51 | |
| 52 | if (!S_ISDIR(dir->i_mode)) |
| 53 | /* |
| 54 | * The parent is not a directory, which means that an extended |
| 55 | * attribute inode is being created. No flags. |
| 56 | */ |
| 57 | return 0; |
| 58 | |
| 59 | flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL); |
| 60 | if (!S_ISDIR(mode)) |
| 61 | /* The "DIRSYNC" flag only applies to directories */ |
| 62 | flags &= ~UBIFS_DIRSYNC_FL; |
| 63 | return flags; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * ubifs_new_inode - allocate new UBIFS inode object. |
| 68 | * @c: UBIFS file-system description object |
| 69 | * @dir: parent directory inode |
| 70 | * @mode: inode mode flags |
| 71 | * |
| 72 | * This function finds an unused inode number, allocates new inode and |
| 73 | * initializes it. Returns new inode in case of success and an error code in |
| 74 | * case of failure. |
| 75 | */ |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 76 | struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir, |
Al Viro | ad44be5 | 2011-07-26 03:12:59 -0400 | [diff] [blame] | 77 | umode_t mode) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 78 | { |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 79 | int err; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 80 | struct inode *inode; |
| 81 | struct ubifs_inode *ui; |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 82 | bool encrypted = false; |
| 83 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 84 | inode = new_inode(c->vfs_sb); |
| 85 | ui = ubifs_inode(inode); |
| 86 | if (!inode) |
| 87 | return ERR_PTR(-ENOMEM); |
| 88 | |
| 89 | /* |
| 90 | * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and |
| 91 | * marking them dirty in file write path (see 'file_update_time()'). |
| 92 | * UBIFS has to fully control "clean <-> dirty" transitions of inodes |
| 93 | * to make budgeting work. |
| 94 | */ |
Artem Bityutskiy | 12e776a | 2011-05-27 15:50:39 +0300 | [diff] [blame] | 95 | inode->i_flags |= S_NOCMTIME; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 96 | |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 97 | inode_init_owner(&init_user_ns, inode, dir, mode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 98 | inode->i_mtime = inode->i_atime = inode->i_ctime = |
Deepa Dinamani | 607a11a | 2017-05-08 15:59:25 -0700 | [diff] [blame] | 99 | current_time(inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 100 | inode->i_mapping->nrpages = 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 101 | |
Eric Biggers | 4c030fa | 2020-09-16 21:11:28 -0700 | [diff] [blame] | 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 | } |
| 107 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 108 | switch (mode & S_IFMT) { |
| 109 | case S_IFREG: |
| 110 | inode->i_mapping->a_ops = &ubifs_file_address_operations; |
| 111 | inode->i_op = &ubifs_file_inode_operations; |
| 112 | inode->i_fop = &ubifs_file_operations; |
| 113 | break; |
| 114 | case S_IFDIR: |
| 115 | inode->i_op = &ubifs_dir_inode_operations; |
| 116 | inode->i_fop = &ubifs_dir_operations; |
| 117 | inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ; |
| 118 | break; |
| 119 | case S_IFLNK: |
| 120 | inode->i_op = &ubifs_symlink_inode_operations; |
| 121 | break; |
| 122 | case S_IFSOCK: |
| 123 | case S_IFIFO: |
| 124 | case S_IFBLK: |
| 125 | case S_IFCHR: |
| 126 | inode->i_op = &ubifs_file_inode_operations; |
| 127 | break; |
| 128 | default: |
| 129 | BUG(); |
| 130 | } |
| 131 | |
| 132 | ui->flags = inherit_flags(dir, mode); |
| 133 | ubifs_set_inode_flags(inode); |
| 134 | if (S_ISREG(mode)) |
| 135 | ui->compr_type = c->default_compr; |
| 136 | else |
| 137 | ui->compr_type = UBIFS_COMPR_NONE; |
| 138 | ui->synced_i_size = 0; |
| 139 | |
| 140 | spin_lock(&c->cnt_lock); |
| 141 | /* Inode number overflow is currently not supported */ |
| 142 | if (c->highest_inum >= INUM_WARN_WATERMARK) { |
| 143 | if (c->highest_inum >= INUM_WATERMARK) { |
| 144 | spin_unlock(&c->cnt_lock); |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 145 | ubifs_err(c, "out of inode numbers"); |
Eric Biggers | 4c030fa | 2020-09-16 21:11:28 -0700 | [diff] [blame] | 146 | err = -EINVAL; |
| 147 | goto out_iput; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 148 | } |
Sheng Yong | 1a7e985d | 2015-04-03 03:13:42 +0000 | [diff] [blame] | 149 | ubifs_warn(c, "running out of inode numbers (current %lu, max %u)", |
Artem Bityutskiy | e84461a | 2008-10-29 12:08:43 +0200 | [diff] [blame] | 150 | (unsigned long)c->highest_inum, INUM_WATERMARK); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | inode->i_ino = ++c->highest_inum; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 154 | /* |
| 155 | * The creation sequence number remains with this inode for its |
| 156 | * lifetime. All nodes for this inode have a greater sequence number, |
| 157 | * and so it is possible to distinguish obsolete nodes belonging to a |
| 158 | * previous incarnation of the same inode number - for example, for the |
| 159 | * purpose of rebuilding the index. |
| 160 | */ |
| 161 | ui->creat_sqnum = ++c->max_sqnum; |
| 162 | spin_unlock(&c->cnt_lock); |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 163 | |
| 164 | if (encrypted) { |
Eric Biggers | 4c030fa | 2020-09-16 21:11:28 -0700 | [diff] [blame] | 165 | err = fscrypt_set_context(inode, NULL); |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 166 | if (err) { |
Eric Biggers | 4c030fa | 2020-09-16 21:11:28 -0700 | [diff] [blame] | 167 | ubifs_err(c, "fscrypt_set_context failed: %i", err); |
| 168 | goto out_iput; |
Richard Weinberger | d475a50 | 2016-10-20 16:47:56 +0200 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 172 | return inode; |
Eric Biggers | 4c030fa | 2020-09-16 21:11:28 -0700 | [diff] [blame] | 173 | |
| 174 | out_iput: |
| 175 | make_bad_inode(inode); |
| 176 | iput(inode); |
| 177 | return ERR_PTR(err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 178 | } |
| 179 | |
Artem Bityutskiy | bb2615d | 2011-05-31 17:47:53 +0300 | [diff] [blame] | 180 | static int dbg_check_name(const struct ubifs_info *c, |
| 181 | const struct ubifs_dent_node *dent, |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 182 | const struct fscrypt_name *nm) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 183 | { |
Artem Bityutskiy | 2b1844a | 2011-06-03 08:31:29 +0300 | [diff] [blame] | 184 | if (!dbg_is_chk_gen(c)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 185 | return 0; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 186 | if (le16_to_cpu(dent->nlen) != fname_len(nm)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 187 | return -EINVAL; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 188 | if (memcmp(dent->name, fname_name(nm), fname_len(nm))) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 189 | return -EINVAL; |
| 190 | return 0; |
| 191 | } |
| 192 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 193 | static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 194 | unsigned int flags) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 195 | { |
| 196 | int err; |
| 197 | union ubifs_key key; |
| 198 | struct inode *inode = NULL; |
Al Viro | 191ac10 | 2018-04-30 20:14:59 -0400 | [diff] [blame] | 199 | struct ubifs_dent_node *dent = NULL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 200 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 201 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 202 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 203 | dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 204 | |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 205 | err = fscrypt_prepare_lookup(dir, dentry, &nm); |
Daniel Rosenberg | bb9cd91 | 2020-11-19 06:09:03 +0000 | [diff] [blame] | 206 | generic_set_encrypted_ci_d_ops(dentry); |
Eric Biggers | b01531d | 2019-03-20 11:39:13 -0700 | [diff] [blame] | 207 | if (err == -ENOENT) |
| 208 | return d_splice_alias(NULL, dentry); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 209 | if (err) |
| 210 | return ERR_PTR(err); |
| 211 | |
| 212 | if (fname_len(&nm) > UBIFS_MAX_NLEN) { |
Al Viro | 191ac10 | 2018-04-30 20:14:59 -0400 | [diff] [blame] | 213 | inode = ERR_PTR(-ENAMETOOLONG); |
| 214 | goto done; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 215 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 216 | |
| 217 | dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 218 | if (!dent) { |
Al Viro | 191ac10 | 2018-04-30 20:14:59 -0400 | [diff] [blame] | 219 | inode = ERR_PTR(-ENOMEM); |
| 220 | goto done; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 221 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 222 | |
Eric Biggers | aec992a | 2020-01-20 14:32:00 -0800 | [diff] [blame] | 223 | if (fname_name(&nm) == NULL) { |
Eric Biggers | f0d07a9 | 2020-01-20 14:31:59 -0800 | [diff] [blame] | 224 | if (nm.hash & ~UBIFS_S_KEY_HASH_MASK) |
| 225 | goto done; /* ENOENT */ |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 226 | dent_key_init_hash(c, &key, dir->i_ino, nm.hash); |
Richard Weinberger | 528e3d1 | 2016-11-11 20:46:06 +0100 | [diff] [blame] | 227 | err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 228 | } else { |
| 229 | dent_key_init(c, &key, dir->i_ino, &nm); |
| 230 | err = ubifs_tnc_lookup_nm(c, &key, dent, &nm); |
| 231 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 232 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 233 | if (err) { |
Al Viro | 191ac10 | 2018-04-30 20:14:59 -0400 | [diff] [blame] | 234 | if (err == -ENOENT) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 235 | dbg_gen("not found"); |
Al Viro | 191ac10 | 2018-04-30 20:14:59 -0400 | [diff] [blame] | 236 | else |
| 237 | inode = ERR_PTR(err); |
| 238 | goto done; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 239 | } |
| 240 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 241 | if (dbg_check_name(c, dent, &nm)) { |
Al Viro | 191ac10 | 2018-04-30 20:14:59 -0400 | [diff] [blame] | 242 | inode = ERR_PTR(-EINVAL); |
| 243 | goto done; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum)); |
| 247 | if (IS_ERR(inode)) { |
| 248 | /* |
| 249 | * This should not happen. Probably the file-system needs |
| 250 | * checking. |
| 251 | */ |
| 252 | err = PTR_ERR(inode); |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 253 | ubifs_err(c, "dead directory entry '%pd', error %d", |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 254 | dentry, err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 255 | ubifs_ro_mode(c, err); |
Al Viro | 191ac10 | 2018-04-30 20:14:59 -0400 | [diff] [blame] | 256 | goto done; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 257 | } |
| 258 | |
Eric Biggers | 50d9fad | 2019-12-09 13:27:21 -0800 | [diff] [blame] | 259 | if (IS_ENCRYPTED(dir) && |
Eric Biggers | 413d5a9 | 2017-04-07 10:58:40 -0700 | [diff] [blame] | 260 | (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && |
| 261 | !fscrypt_has_permitted_context(dir, inode)) { |
| 262 | ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu", |
| 263 | dir->i_ino, inode->i_ino); |
Al Viro | 191ac10 | 2018-04-30 20:14:59 -0400 | [diff] [blame] | 264 | iput(inode); |
| 265 | inode = ERR_PTR(-EPERM); |
Eric Biggers | 413d5a9 | 2017-04-07 10:58:40 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 268 | done: |
| 269 | kfree(dent); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 270 | fscrypt_free_filename(&nm); |
Al Viro | 191ac10 | 2018-04-30 20:14:59 -0400 | [diff] [blame] | 271 | return d_splice_alias(inode, dentry); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 272 | } |
| 273 | |
Eric Biggers | 76786a0f0 | 2020-11-17 23:56:08 -0800 | [diff] [blame] | 274 | static int ubifs_prepare_create(struct inode *dir, struct dentry *dentry, |
| 275 | struct fscrypt_name *nm) |
| 276 | { |
| 277 | if (fscrypt_is_nokey_name(dentry)) |
| 278 | return -ENOKEY; |
| 279 | |
| 280 | return fscrypt_setup_filename(dir, &dentry->d_name, 0, nm); |
| 281 | } |
| 282 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 283 | static int ubifs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 284 | struct dentry *dentry, umode_t mode, bool excl) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 285 | { |
| 286 | struct inode *inode; |
| 287 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 288 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, |
| 289 | .dirtied_ino = 1 }; |
| 290 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 291 | struct fscrypt_name nm; |
| 292 | int err, sz_change; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 293 | |
| 294 | /* |
| 295 | * Budget request settings: new inode, new direntry, changing the |
| 296 | * parent directory inode. |
| 297 | */ |
| 298 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 299 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
| 300 | dentry, mode, dir->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 301 | |
| 302 | err = ubifs_budget_space(c, &req); |
| 303 | if (err) |
| 304 | return err; |
| 305 | |
Eric Biggers | 76786a0f0 | 2020-11-17 23:56:08 -0800 | [diff] [blame] | 306 | err = ubifs_prepare_create(dir, dentry, &nm); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 307 | if (err) |
| 308 | goto out_budg; |
| 309 | |
| 310 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 311 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 312 | inode = ubifs_new_inode(c, dir, mode); |
| 313 | if (IS_ERR(inode)) { |
| 314 | err = PTR_ERR(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 315 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 316 | } |
| 317 | |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 318 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
| 319 | if (err) |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 320 | goto out_inode; |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 321 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 322 | mutex_lock(&dir_ui->ui_mutex); |
| 323 | dir->i_size += sz_change; |
| 324 | dir_ui->ui_size = dir->i_size; |
| 325 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 326 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 327 | if (err) |
| 328 | goto out_cancel; |
| 329 | mutex_unlock(&dir_ui->ui_mutex); |
| 330 | |
| 331 | ubifs_release_budget(c, &req); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 332 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 333 | insert_inode_hash(inode); |
| 334 | d_instantiate(dentry, inode); |
| 335 | return 0; |
| 336 | |
| 337 | out_cancel: |
| 338 | dir->i_size -= sz_change; |
| 339 | dir_ui->ui_size = dir->i_size; |
| 340 | mutex_unlock(&dir_ui->ui_mutex); |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 341 | out_inode: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 342 | make_bad_inode(inode); |
| 343 | iput(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 344 | out_fname: |
| 345 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 346 | out_budg: |
| 347 | ubifs_release_budget(c, &req); |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 348 | ubifs_err(c, "cannot create regular file, error %d", err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 349 | return err; |
| 350 | } |
| 351 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 352 | static int do_tmpfile(struct inode *dir, struct dentry *dentry, |
| 353 | umode_t mode, struct inode **whiteout) |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 354 | { |
| 355 | struct inode *inode; |
| 356 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
| 357 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1}; |
| 358 | struct ubifs_budget_req ino_req = { .dirtied_ino = 1 }; |
| 359 | struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir); |
| 360 | int err, instantiated = 0; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 361 | struct fscrypt_name nm; |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * Budget request settings: new dirty inode, new direntry, |
| 365 | * budget for dirtied inode will be released via writeback. |
| 366 | */ |
| 367 | |
| 368 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
| 369 | dentry, mode, dir->i_ino); |
| 370 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 371 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm); |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 372 | if (err) |
| 373 | return err; |
| 374 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 375 | err = ubifs_budget_space(c, &req); |
| 376 | if (err) { |
| 377 | fscrypt_free_filename(&nm); |
| 378 | return err; |
| 379 | } |
| 380 | |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 381 | err = ubifs_budget_space(c, &ino_req); |
| 382 | if (err) { |
| 383 | ubifs_release_budget(c, &req); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 384 | fscrypt_free_filename(&nm); |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 385 | return err; |
| 386 | } |
| 387 | |
| 388 | inode = ubifs_new_inode(c, dir, mode); |
| 389 | if (IS_ERR(inode)) { |
| 390 | err = PTR_ERR(inode); |
| 391 | goto out_budg; |
| 392 | } |
| 393 | ui = ubifs_inode(inode); |
| 394 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 395 | if (whiteout) { |
| 396 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 397 | ubifs_assert(c, inode->i_op == &ubifs_file_inode_operations); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 400 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
| 401 | if (err) |
| 402 | goto out_inode; |
| 403 | |
| 404 | mutex_lock(&ui->ui_mutex); |
| 405 | insert_inode_hash(inode); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 406 | |
| 407 | if (whiteout) { |
| 408 | mark_inode_dirty(inode); |
| 409 | drop_nlink(inode); |
| 410 | *whiteout = inode; |
| 411 | } else { |
| 412 | d_tmpfile(dentry, inode); |
| 413 | } |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 414 | ubifs_assert(c, ui->dirty); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 415 | |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 416 | instantiated = 1; |
| 417 | mutex_unlock(&ui->ui_mutex); |
| 418 | |
| 419 | mutex_lock(&dir_ui->ui_mutex); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 420 | err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0); |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 421 | if (err) |
| 422 | goto out_cancel; |
| 423 | mutex_unlock(&dir_ui->ui_mutex); |
| 424 | |
| 425 | ubifs_release_budget(c, &req); |
| 426 | |
| 427 | return 0; |
| 428 | |
| 429 | out_cancel: |
| 430 | mutex_unlock(&dir_ui->ui_mutex); |
| 431 | out_inode: |
| 432 | make_bad_inode(inode); |
| 433 | if (!instantiated) |
| 434 | iput(inode); |
| 435 | out_budg: |
| 436 | ubifs_release_budget(c, &req); |
| 437 | if (!instantiated) |
| 438 | ubifs_release_budget(c, &ino_req); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 439 | fscrypt_free_filename(&nm); |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 440 | ubifs_err(c, "cannot create temporary file, error %d", err); |
| 441 | return err; |
| 442 | } |
| 443 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 444 | static int ubifs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, |
| 445 | struct dentry *dentry, umode_t mode) |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 446 | { |
| 447 | return do_tmpfile(dir, dentry, mode, NULL); |
| 448 | } |
| 449 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 450 | /** |
| 451 | * vfs_dent_type - get VFS directory entry type. |
| 452 | * @type: UBIFS directory entry type |
| 453 | * |
| 454 | * This function converts UBIFS directory entry type into VFS directory entry |
| 455 | * type. |
| 456 | */ |
| 457 | static unsigned int vfs_dent_type(uint8_t type) |
| 458 | { |
| 459 | switch (type) { |
| 460 | case UBIFS_ITYPE_REG: |
| 461 | return DT_REG; |
| 462 | case UBIFS_ITYPE_DIR: |
| 463 | return DT_DIR; |
| 464 | case UBIFS_ITYPE_LNK: |
| 465 | return DT_LNK; |
| 466 | case UBIFS_ITYPE_BLK: |
| 467 | return DT_BLK; |
| 468 | case UBIFS_ITYPE_CHR: |
| 469 | return DT_CHR; |
| 470 | case UBIFS_ITYPE_FIFO: |
| 471 | return DT_FIFO; |
| 472 | case UBIFS_ITYPE_SOCK: |
| 473 | return DT_SOCK; |
| 474 | default: |
| 475 | BUG(); |
| 476 | } |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * The classical Unix view for directory is that it is a linear array of |
| 482 | * (name, inode number) entries. Linux/VFS assumes this model as well. |
| 483 | * Particularly, 'readdir()' call wants us to return a directory entry offset |
| 484 | * which later may be used to continue 'readdir()'ing the directory or to |
| 485 | * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this |
| 486 | * model because directory entries are identified by keys, which may collide. |
| 487 | * |
| 488 | * UBIFS uses directory entry hash value for directory offsets, so |
| 489 | * 'seekdir()'/'telldir()' may not always work because of possible key |
| 490 | * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work |
| 491 | * properly by means of saving full directory entry name in the private field |
| 492 | * of the file description object. |
| 493 | * |
| 494 | * This means that UBIFS cannot support NFS which requires full |
| 495 | * 'seekdir()'/'telldir()' support. |
| 496 | */ |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 497 | static int ubifs_readdir(struct file *file, struct dir_context *ctx) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 498 | { |
Richard Weinberger | ba75d57 | 2016-12-14 11:09:25 +0100 | [diff] [blame] | 499 | int fstr_real_len = 0, err = 0; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 500 | struct fscrypt_name nm; |
| 501 | struct fscrypt_str fstr = {0}; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 502 | union ubifs_key key; |
| 503 | struct ubifs_dent_node *dent; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 504 | struct inode *dir = file_inode(file); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 505 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
Eric Biggers | 50d9fad | 2019-12-09 13:27:21 -0800 | [diff] [blame] | 506 | bool encrypted = IS_ENCRYPTED(dir); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 507 | |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 508 | dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 509 | |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 510 | if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 511 | /* |
| 512 | * The directory was seek'ed to a senseless position or there |
| 513 | * are no more entries. |
| 514 | */ |
| 515 | return 0; |
| 516 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 517 | if (encrypted) { |
Eric Biggers | ec0caa97 | 2020-12-02 18:20:37 -0800 | [diff] [blame] | 518 | err = fscrypt_prepare_readdir(dir); |
Eric Biggers | 3b1ada55 | 2019-12-09 13:23:48 -0800 | [diff] [blame] | 519 | if (err) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 520 | return err; |
| 521 | |
Jeff Layton | 8b10fe6 | 2020-08-10 10:21:39 -0400 | [diff] [blame] | 522 | err = fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN, &fstr); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 523 | if (err) |
| 524 | return err; |
| 525 | |
| 526 | fstr_real_len = fstr.len; |
| 527 | } |
| 528 | |
Artem Bityutskiy | 605c912 | 2013-06-28 14:15:15 +0300 | [diff] [blame] | 529 | if (file->f_version == 0) { |
| 530 | /* |
| 531 | * The file was seek'ed, which means that @file->private_data |
| 532 | * is now invalid. This may also be just the first |
| 533 | * 'ubifs_readdir()' invocation, in which case |
| 534 | * @file->private_data is NULL, and the below code is |
| 535 | * basically a no-op. |
| 536 | */ |
| 537 | kfree(file->private_data); |
| 538 | file->private_data = NULL; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * 'generic_file_llseek()' unconditionally sets @file->f_version to |
| 543 | * zero, and we use this for detecting whether the file was seek'ed. |
| 544 | */ |
| 545 | file->f_version = 1; |
| 546 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 547 | /* File positions 0 and 1 correspond to "." and ".." */ |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 548 | if (ctx->pos < 2) { |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 549 | ubifs_assert(c, !file->private_data); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 550 | if (!dir_emit_dots(file, ctx)) { |
| 551 | if (encrypted) |
| 552 | fscrypt_fname_free_buffer(&fstr); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 553 | return 0; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 554 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 555 | |
| 556 | /* Find the first entry in TNC and save it */ |
| 557 | lowest_dent_key(c, &key, dir->i_ino); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 558 | fname_len(&nm) = 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 559 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 560 | if (IS_ERR(dent)) { |
| 561 | err = PTR_ERR(dent); |
| 562 | goto out; |
| 563 | } |
| 564 | |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 565 | ctx->pos = key_hash_flash(c, &dent->key); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 566 | file->private_data = dent; |
| 567 | } |
| 568 | |
| 569 | dent = file->private_data; |
| 570 | if (!dent) { |
| 571 | /* |
| 572 | * The directory was seek'ed to and is now readdir'ed. |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 573 | * Find the entry corresponding to @ctx->pos or the closest one. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 574 | */ |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 575 | dent_key_init_hash(c, &key, dir->i_ino, ctx->pos); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 576 | fname_len(&nm) = 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 577 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 578 | if (IS_ERR(dent)) { |
| 579 | err = PTR_ERR(dent); |
| 580 | goto out; |
| 581 | } |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 582 | ctx->pos = key_hash_flash(c, &dent->key); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 583 | file->private_data = dent; |
| 584 | } |
| 585 | |
| 586 | while (1) { |
Hyunchul Lee | b20e2d9 | 2017-03-15 10:31:03 +0900 | [diff] [blame] | 587 | dbg_gen("ino %llu, new f_pos %#x", |
| 588 | (unsigned long long)le64_to_cpu(dent->inum), |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 589 | key_hash_flash(c, &dent->key)); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 590 | ubifs_assert(c, le64_to_cpu(dent->ch.sqnum) > |
Harvey Harrison | 0ecb952 | 2008-10-24 10:52:57 -0700 | [diff] [blame] | 591 | ubifs_inode(dir)->creat_sqnum); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 592 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 593 | fname_len(&nm) = le16_to_cpu(dent->nlen); |
| 594 | fname_name(&nm) = dent->name; |
| 595 | |
| 596 | if (encrypted) { |
| 597 | fstr.len = fstr_real_len; |
| 598 | |
Richard Weinberger | 528e3d1 | 2016-11-11 20:46:06 +0100 | [diff] [blame] | 599 | err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c, |
| 600 | &dent->key), |
| 601 | le32_to_cpu(dent->cookie), |
| 602 | &nm.disk_name, &fstr); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 603 | if (err) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 604 | goto out; |
| 605 | } else { |
| 606 | fstr.len = fname_len(&nm); |
| 607 | fstr.name = fname_name(&nm); |
| 608 | } |
| 609 | |
| 610 | if (!dir_emit(ctx, fstr.name, fstr.len, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 611 | le64_to_cpu(dent->inum), |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 612 | vfs_dent_type(dent->type))) { |
| 613 | if (encrypted) |
| 614 | fscrypt_fname_free_buffer(&fstr); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 615 | return 0; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 616 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 617 | |
| 618 | /* Switch to the next entry */ |
| 619 | key_read(c, &dent->key, &key); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 620 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 621 | if (IS_ERR(dent)) { |
| 622 | err = PTR_ERR(dent); |
| 623 | goto out; |
| 624 | } |
| 625 | |
| 626 | kfree(file->private_data); |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 627 | ctx->pos = key_hash_flash(c, &dent->key); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 628 | file->private_data = dent; |
| 629 | cond_resched(); |
| 630 | } |
| 631 | |
| 632 | out: |
Richard Weinberger | aeeb14f | 2015-10-12 23:35:36 +0200 | [diff] [blame] | 633 | kfree(file->private_data); |
| 634 | file->private_data = NULL; |
| 635 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 636 | if (encrypted) |
| 637 | fscrypt_fname_free_buffer(&fstr); |
| 638 | |
Richard Weinberger | c83ed4c | 2016-10-19 12:43:07 +0200 | [diff] [blame] | 639 | if (err != -ENOENT) |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 640 | ubifs_err(c, "cannot find next direntry, error %d", err); |
Richard Weinberger | a00052a | 2016-10-28 11:49:03 +0200 | [diff] [blame] | 641 | else |
| 642 | /* |
| 643 | * -ENOENT is a non-fatal error in this context, the TNC uses |
| 644 | * it to indicate that the cursor moved past the current directory |
| 645 | * and readdir() has to stop. |
| 646 | */ |
| 647 | err = 0; |
| 648 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 649 | |
Artem Bityutskiy | 605c912 | 2013-06-28 14:15:15 +0300 | [diff] [blame] | 650 | /* 2 is a special value indicating that there are no more direntries */ |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 651 | ctx->pos = 2; |
Richard Weinberger | c83ed4c | 2016-10-19 12:43:07 +0200 | [diff] [blame] | 652 | return err; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 653 | } |
| 654 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 655 | /* Free saved readdir() state when the directory is closed */ |
| 656 | static int ubifs_dir_release(struct inode *dir, struct file *file) |
| 657 | { |
| 658 | kfree(file->private_data); |
| 659 | file->private_data = NULL; |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | /** |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 664 | * lock_2_inodes - a wrapper for locking two UBIFS inodes. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 665 | * @inode1: first inode |
| 666 | * @inode2: second inode |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 667 | * |
| 668 | * We do not implement any tricks to guarantee strict lock ordering, because |
| 669 | * VFS has already done it for us on the @i_mutex. So this is just a simple |
| 670 | * wrapper function. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 671 | */ |
| 672 | static void lock_2_inodes(struct inode *inode1, struct inode *inode2) |
| 673 | { |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 674 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
| 675 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /** |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 679 | * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 680 | * @inode1: first inode |
| 681 | * @inode2: second inode |
| 682 | */ |
| 683 | static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) |
| 684 | { |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 685 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 686 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static int ubifs_link(struct dentry *old_dentry, struct inode *dir, |
| 690 | struct dentry *dentry) |
| 691 | { |
| 692 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 693 | struct inode *inode = d_inode(old_dentry); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 694 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 695 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
| 696 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); |
| 697 | struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2, |
Artem Bityutskiy | dab4b4d | 2008-07-24 14:52:45 +0300 | [diff] [blame] | 698 | .dirtied_ino_d = ALIGN(ui->data_len, 8) }; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 699 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 700 | |
| 701 | /* |
| 702 | * Budget request settings: new direntry, changing the target inode, |
| 703 | * changing the parent inode. |
| 704 | */ |
| 705 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 706 | dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu", |
| 707 | dentry, inode->i_ino, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 708 | inode->i_nlink, dir->i_ino); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 709 | ubifs_assert(c, inode_is_locked(dir)); |
| 710 | ubifs_assert(c, inode_is_locked(inode)); |
Hunter Adrian | 8b3884a | 2009-05-14 06:32:30 +0200 | [diff] [blame] | 711 | |
Eric Biggers | 5653878 | 2017-11-29 12:43:14 -0800 | [diff] [blame] | 712 | err = fscrypt_prepare_link(old_dentry, dir, dentry); |
| 713 | if (err) |
| 714 | return err; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 715 | |
| 716 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm); |
| 717 | if (err) |
| 718 | return err; |
Richard Weinberger | ac7e47a | 2016-09-29 20:00:38 +0200 | [diff] [blame] | 719 | |
Artem Bityutskiy | d808efb | 2011-05-31 18:14:38 +0300 | [diff] [blame] | 720 | err = dbg_check_synced_i_size(c, inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 721 | if (err) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 722 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 723 | |
| 724 | err = ubifs_budget_space(c, &req); |
| 725 | if (err) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 726 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 727 | |
| 728 | lock_2_inodes(dir, inode); |
Richard Weinberger | 32fe905 | 2017-03-30 10:50:49 +0200 | [diff] [blame] | 729 | |
| 730 | /* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */ |
| 731 | if (inode->i_nlink == 0) |
| 732 | ubifs_delete_orphan(c, inode->i_ino); |
| 733 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 734 | inc_nlink(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 735 | ihold(inode); |
Deepa Dinamani | 607a11a | 2017-05-08 15:59:25 -0700 | [diff] [blame] | 736 | inode->i_ctime = current_time(inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 737 | dir->i_size += sz_change; |
| 738 | dir_ui->ui_size = dir->i_size; |
| 739 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 740 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 741 | if (err) |
| 742 | goto out_cancel; |
| 743 | unlock_2_inodes(dir, inode); |
| 744 | |
| 745 | ubifs_release_budget(c, &req); |
| 746 | d_instantiate(dentry, inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 747 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 748 | return 0; |
| 749 | |
| 750 | out_cancel: |
| 751 | dir->i_size -= sz_change; |
| 752 | dir_ui->ui_size = dir->i_size; |
| 753 | drop_nlink(inode); |
Richard Weinberger | 32fe905 | 2017-03-30 10:50:49 +0200 | [diff] [blame] | 754 | if (inode->i_nlink == 0) |
| 755 | ubifs_add_orphan(c, inode->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 756 | unlock_2_inodes(dir, inode); |
| 757 | ubifs_release_budget(c, &req); |
| 758 | iput(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 759 | out_fname: |
| 760 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 761 | return err; |
| 762 | } |
| 763 | |
| 764 | static int ubifs_unlink(struct inode *dir, struct dentry *dentry) |
| 765 | { |
| 766 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 767 | struct inode *inode = d_inode(dentry); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 768 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 769 | int err, sz_change, budgeted = 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 770 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 771 | unsigned int saved_nlink = inode->i_nlink; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 772 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 773 | |
| 774 | /* |
| 775 | * Budget request settings: deletion direntry, deletion inode (+1 for |
| 776 | * @dirtied_ino), changing the parent directory inode. If budgeting |
| 777 | * fails, go ahead anyway because we have extra space reserved for |
| 778 | * deletions. |
| 779 | */ |
| 780 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 781 | dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu", |
| 782 | dentry, inode->i_ino, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 783 | inode->i_nlink, dir->i_ino); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 784 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 785 | err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm); |
| 786 | if (err) |
| 787 | return err; |
| 788 | |
Richard Weinberger | 9ca2d73 | 2019-04-05 00:34:38 +0200 | [diff] [blame] | 789 | err = ubifs_purge_xattrs(inode); |
| 790 | if (err) |
| 791 | return err; |
| 792 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 793 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 794 | |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 795 | ubifs_assert(c, inode_is_locked(dir)); |
| 796 | ubifs_assert(c, inode_is_locked(inode)); |
Artem Bityutskiy | d808efb | 2011-05-31 18:14:38 +0300 | [diff] [blame] | 797 | err = dbg_check_synced_i_size(c, inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 798 | if (err) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 799 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 800 | |
| 801 | err = ubifs_budget_space(c, &req); |
| 802 | if (err) { |
| 803 | if (err != -ENOSPC) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 804 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 805 | budgeted = 0; |
| 806 | } |
| 807 | |
| 808 | lock_2_inodes(dir, inode); |
Deepa Dinamani | 607a11a | 2017-05-08 15:59:25 -0700 | [diff] [blame] | 809 | inode->i_ctime = current_time(dir); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 810 | drop_nlink(inode); |
| 811 | dir->i_size -= sz_change; |
| 812 | dir_ui->ui_size = dir->i_size; |
| 813 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 814 | err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 815 | if (err) |
| 816 | goto out_cancel; |
| 817 | unlock_2_inodes(dir, inode); |
| 818 | |
| 819 | if (budgeted) |
| 820 | ubifs_release_budget(c, &req); |
| 821 | else { |
| 822 | /* We've deleted something - clean the "no space" flags */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 823 | c->bi.nospace = c->bi.nospace_rp = 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 824 | smp_wmb(); |
| 825 | } |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 826 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 827 | return 0; |
| 828 | |
| 829 | out_cancel: |
| 830 | dir->i_size += sz_change; |
| 831 | dir_ui->ui_size = dir->i_size; |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 832 | set_nlink(inode, saved_nlink); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 833 | unlock_2_inodes(dir, inode); |
| 834 | if (budgeted) |
| 835 | ubifs_release_budget(c, &req); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 836 | out_fname: |
| 837 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 838 | return err; |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * check_dir_empty - check if a directory is empty or not. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 843 | * @dir: VFS inode object of the directory to check |
| 844 | * |
| 845 | * This function checks if directory @dir is empty. Returns zero if the |
| 846 | * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes |
Randy Dunlap | b8f1da9 | 2020-08-04 19:49:35 -0700 | [diff] [blame] | 847 | * in case of errors. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 848 | */ |
Richard Weinberger | f6337d8 | 2016-09-19 20:54:19 +0200 | [diff] [blame] | 849 | int ubifs_check_dir_empty(struct inode *dir) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 850 | { |
Richard Weinberger | f6337d8 | 2016-09-19 20:54:19 +0200 | [diff] [blame] | 851 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 852 | struct fscrypt_name nm = { 0 }; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 853 | struct ubifs_dent_node *dent; |
| 854 | union ubifs_key key; |
| 855 | int err; |
| 856 | |
| 857 | lowest_dent_key(c, &key, dir->i_ino); |
| 858 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 859 | if (IS_ERR(dent)) { |
| 860 | err = PTR_ERR(dent); |
| 861 | if (err == -ENOENT) |
| 862 | err = 0; |
| 863 | } else { |
| 864 | kfree(dent); |
| 865 | err = -ENOTEMPTY; |
| 866 | } |
| 867 | return err; |
| 868 | } |
| 869 | |
| 870 | static int ubifs_rmdir(struct inode *dir, struct dentry *dentry) |
| 871 | { |
| 872 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 873 | struct inode *inode = d_inode(dentry); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 874 | int err, sz_change, budgeted = 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 875 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
| 876 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 877 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 878 | |
| 879 | /* |
| 880 | * Budget request settings: deletion direntry, deletion inode and |
| 881 | * changing the parent inode. If budgeting fails, go ahead anyway |
| 882 | * because we have extra space reserved for deletions. |
| 883 | */ |
| 884 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 885 | dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry, |
| 886 | inode->i_ino, dir->i_ino); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 887 | ubifs_assert(c, inode_is_locked(dir)); |
| 888 | ubifs_assert(c, inode_is_locked(inode)); |
Richard Weinberger | f6337d8 | 2016-09-19 20:54:19 +0200 | [diff] [blame] | 889 | err = ubifs_check_dir_empty(d_inode(dentry)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 890 | if (err) |
| 891 | return err; |
| 892 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 893 | err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm); |
| 894 | if (err) |
| 895 | return err; |
| 896 | |
Richard Weinberger | 9ca2d73 | 2019-04-05 00:34:38 +0200 | [diff] [blame] | 897 | err = ubifs_purge_xattrs(inode); |
| 898 | if (err) |
| 899 | return err; |
| 900 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 901 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 902 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 903 | err = ubifs_budget_space(c, &req); |
| 904 | if (err) { |
| 905 | if (err != -ENOSPC) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 906 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 907 | budgeted = 0; |
| 908 | } |
| 909 | |
| 910 | lock_2_inodes(dir, inode); |
Deepa Dinamani | 607a11a | 2017-05-08 15:59:25 -0700 | [diff] [blame] | 911 | inode->i_ctime = current_time(dir); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 912 | clear_nlink(inode); |
| 913 | drop_nlink(dir); |
| 914 | dir->i_size -= sz_change; |
| 915 | dir_ui->ui_size = dir->i_size; |
| 916 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 917 | err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 918 | if (err) |
| 919 | goto out_cancel; |
| 920 | unlock_2_inodes(dir, inode); |
| 921 | |
| 922 | if (budgeted) |
| 923 | ubifs_release_budget(c, &req); |
| 924 | else { |
| 925 | /* We've deleted something - clean the "no space" flags */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 926 | c->bi.nospace = c->bi.nospace_rp = 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 927 | smp_wmb(); |
| 928 | } |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 929 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 930 | return 0; |
| 931 | |
| 932 | out_cancel: |
| 933 | dir->i_size += sz_change; |
| 934 | dir_ui->ui_size = dir->i_size; |
| 935 | inc_nlink(dir); |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 936 | set_nlink(inode, 2); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 937 | unlock_2_inodes(dir, inode); |
| 938 | if (budgeted) |
| 939 | ubifs_release_budget(c, &req); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 940 | out_fname: |
| 941 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 942 | return err; |
| 943 | } |
| 944 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 945 | static int ubifs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 946 | struct dentry *dentry, umode_t mode) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 947 | { |
| 948 | struct inode *inode; |
| 949 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
| 950 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 951 | int err, sz_change; |
Artem Bityutskiy | 182854b | 2008-07-18 18:54:29 +0300 | [diff] [blame] | 952 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 }; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 953 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 954 | |
| 955 | /* |
| 956 | * Budget request settings: new inode, new direntry and changing parent |
| 957 | * directory inode. |
| 958 | */ |
| 959 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 960 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
| 961 | dentry, mode, dir->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 962 | |
| 963 | err = ubifs_budget_space(c, &req); |
| 964 | if (err) |
| 965 | return err; |
| 966 | |
Eric Biggers | 76786a0f0 | 2020-11-17 23:56:08 -0800 | [diff] [blame] | 967 | err = ubifs_prepare_create(dir, dentry, &nm); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 968 | if (err) |
| 969 | goto out_budg; |
| 970 | |
| 971 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 972 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 973 | inode = ubifs_new_inode(c, dir, S_IFDIR | mode); |
| 974 | if (IS_ERR(inode)) { |
| 975 | err = PTR_ERR(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 976 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 977 | } |
| 978 | |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 979 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
| 980 | if (err) |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 981 | goto out_inode; |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 982 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 983 | mutex_lock(&dir_ui->ui_mutex); |
| 984 | insert_inode_hash(inode); |
| 985 | inc_nlink(inode); |
| 986 | inc_nlink(dir); |
| 987 | dir->i_size += sz_change; |
| 988 | dir_ui->ui_size = dir->i_size; |
| 989 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 990 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 991 | if (err) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 992 | ubifs_err(c, "cannot create directory, error %d", err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 993 | goto out_cancel; |
| 994 | } |
| 995 | mutex_unlock(&dir_ui->ui_mutex); |
| 996 | |
| 997 | ubifs_release_budget(c, &req); |
| 998 | d_instantiate(dentry, inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 999 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1000 | return 0; |
| 1001 | |
| 1002 | out_cancel: |
| 1003 | dir->i_size -= sz_change; |
| 1004 | dir_ui->ui_size = dir->i_size; |
| 1005 | drop_nlink(dir); |
| 1006 | mutex_unlock(&dir_ui->ui_mutex); |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 1007 | out_inode: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1008 | make_bad_inode(inode); |
| 1009 | iput(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1010 | out_fname: |
| 1011 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1012 | out_budg: |
| 1013 | ubifs_release_budget(c, &req); |
| 1014 | return err; |
| 1015 | } |
| 1016 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 1017 | static int ubifs_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 1018 | struct dentry *dentry, umode_t mode, dev_t rdev) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1019 | { |
| 1020 | struct inode *inode; |
| 1021 | struct ubifs_inode *ui; |
| 1022 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
| 1023 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
| 1024 | union ubifs_dev_desc *dev = NULL; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1025 | int sz_change; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1026 | int err, devlen = 0; |
| 1027 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, |
Artem Bityutskiy | dab4b4d | 2008-07-24 14:52:45 +0300 | [diff] [blame] | 1028 | .dirtied_ino = 1 }; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1029 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1030 | |
| 1031 | /* |
| 1032 | * Budget request settings: new inode, new direntry and changing parent |
| 1033 | * directory inode. |
| 1034 | */ |
| 1035 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 1036 | dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1037 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1038 | if (S_ISBLK(mode) || S_ISCHR(mode)) { |
| 1039 | dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); |
| 1040 | if (!dev) |
| 1041 | return -ENOMEM; |
| 1042 | devlen = ubifs_encode_dev(dev, rdev); |
| 1043 | } |
| 1044 | |
Hyunchul Lee | 4d35ca4 | 2017-05-17 08:57:18 +0900 | [diff] [blame] | 1045 | req.new_ino_d = ALIGN(devlen, 8); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1046 | err = ubifs_budget_space(c, &req); |
| 1047 | if (err) { |
| 1048 | kfree(dev); |
| 1049 | return err; |
| 1050 | } |
| 1051 | |
Eric Biggers | 76786a0f0 | 2020-11-17 23:56:08 -0800 | [diff] [blame] | 1052 | err = ubifs_prepare_create(dir, dentry, &nm); |
Richard Weinberger | 63ed657 | 2017-02-10 17:46:01 +0100 | [diff] [blame] | 1053 | if (err) { |
| 1054 | kfree(dev); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1055 | goto out_budg; |
Richard Weinberger | 63ed657 | 2017-02-10 17:46:01 +0100 | [diff] [blame] | 1056 | } |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1057 | |
| 1058 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 1059 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1060 | inode = ubifs_new_inode(c, dir, mode); |
| 1061 | if (IS_ERR(inode)) { |
| 1062 | kfree(dev); |
| 1063 | err = PTR_ERR(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1064 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | init_special_inode(inode, inode->i_mode, rdev); |
| 1068 | inode->i_size = ubifs_inode(inode)->ui_size = devlen; |
| 1069 | ui = ubifs_inode(inode); |
| 1070 | ui->data = dev; |
| 1071 | ui->data_len = devlen; |
| 1072 | |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 1073 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
| 1074 | if (err) |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 1075 | goto out_inode; |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 1076 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1077 | mutex_lock(&dir_ui->ui_mutex); |
| 1078 | dir->i_size += sz_change; |
| 1079 | dir_ui->ui_size = dir->i_size; |
| 1080 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1081 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1082 | if (err) |
| 1083 | goto out_cancel; |
| 1084 | mutex_unlock(&dir_ui->ui_mutex); |
| 1085 | |
| 1086 | ubifs_release_budget(c, &req); |
| 1087 | insert_inode_hash(inode); |
| 1088 | d_instantiate(dentry, inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1089 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1090 | return 0; |
| 1091 | |
| 1092 | out_cancel: |
| 1093 | dir->i_size -= sz_change; |
| 1094 | dir_ui->ui_size = dir->i_size; |
| 1095 | mutex_unlock(&dir_ui->ui_mutex); |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 1096 | out_inode: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1097 | make_bad_inode(inode); |
| 1098 | iput(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1099 | out_fname: |
| 1100 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1101 | out_budg: |
| 1102 | ubifs_release_budget(c, &req); |
| 1103 | return err; |
| 1104 | } |
| 1105 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 1106 | static int ubifs_symlink(struct user_namespace *mnt_userns, struct inode *dir, |
| 1107 | struct dentry *dentry, const char *symname) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1108 | { |
| 1109 | struct inode *inode; |
| 1110 | struct ubifs_inode *ui; |
| 1111 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
| 1112 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
Richard Weinberger | 00ee8b6 | 2018-06-11 23:41:09 +0200 | [diff] [blame] | 1113 | int err, sz_change, len = strlen(symname); |
Eric Biggers | 0e4dda2 | 2018-01-11 23:27:00 -0500 | [diff] [blame] | 1114 | struct fscrypt_str disk_link; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1115 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, |
Artem Bityutskiy | dab4b4d | 2008-07-24 14:52:45 +0300 | [diff] [blame] | 1116 | .new_ino_d = ALIGN(len, 8), |
| 1117 | .dirtied_ino = 1 }; |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1118 | struct fscrypt_name nm; |
| 1119 | |
Eric Biggers | 0e4dda2 | 2018-01-11 23:27:00 -0500 | [diff] [blame] | 1120 | dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry, |
| 1121 | symname, dir->i_ino); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1122 | |
Eric Biggers | 0e4dda2 | 2018-01-11 23:27:00 -0500 | [diff] [blame] | 1123 | err = fscrypt_prepare_symlink(dir, symname, len, UBIFS_MAX_INO_DATA, |
| 1124 | &disk_link); |
| 1125 | if (err) |
| 1126 | return err; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1127 | |
| 1128 | /* |
| 1129 | * Budget request settings: new inode, new direntry and changing parent |
| 1130 | * directory inode. |
| 1131 | */ |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1132 | err = ubifs_budget_space(c, &req); |
| 1133 | if (err) |
| 1134 | return err; |
| 1135 | |
Eric Biggers | 76786a0f0 | 2020-11-17 23:56:08 -0800 | [diff] [blame] | 1136 | err = ubifs_prepare_create(dir, dentry, &nm); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1137 | if (err) |
| 1138 | goto out_budg; |
| 1139 | |
Richard Weinberger | 00ee8b6 | 2018-06-11 23:41:09 +0200 | [diff] [blame] | 1140 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 1141 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1142 | inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO); |
| 1143 | if (IS_ERR(inode)) { |
| 1144 | err = PTR_ERR(inode); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1145 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | ui = ubifs_inode(inode); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1149 | ui->data = kmalloc(disk_link.len, GFP_NOFS); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1150 | if (!ui->data) { |
| 1151 | err = -ENOMEM; |
| 1152 | goto out_inode; |
| 1153 | } |
| 1154 | |
Eric Biggers | 0e4dda2 | 2018-01-11 23:27:00 -0500 | [diff] [blame] | 1155 | if (IS_ENCRYPTED(inode)) { |
| 1156 | disk_link.name = ui->data; /* encrypt directly into ui->data */ |
| 1157 | err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link); |
Eric Biggers | 6b46d44 | 2018-01-11 23:27:00 -0500 | [diff] [blame] | 1158 | if (err) |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1159 | goto out_inode; |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1160 | } else { |
Eric Biggers | 0e4dda2 | 2018-01-11 23:27:00 -0500 | [diff] [blame] | 1161 | memcpy(ui->data, disk_link.name, disk_link.len); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1162 | inode->i_link = ui->data; |
| 1163 | } |
| 1164 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1165 | /* |
| 1166 | * The terminating zero byte is not written to the flash media and it |
| 1167 | * is put just to make later in-memory string processing simpler. Thus, |
Eric Biggers | 0e4dda2 | 2018-01-11 23:27:00 -0500 | [diff] [blame] | 1168 | * data length is @disk_link.len - 1, not @disk_link.len. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1169 | */ |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1170 | ui->data_len = disk_link.len - 1; |
| 1171 | inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1172 | |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 1173 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
| 1174 | if (err) |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 1175 | goto out_inode; |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 1176 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1177 | mutex_lock(&dir_ui->ui_mutex); |
| 1178 | dir->i_size += sz_change; |
| 1179 | dir_ui->ui_size = dir->i_size; |
| 1180 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1181 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1182 | if (err) |
| 1183 | goto out_cancel; |
| 1184 | mutex_unlock(&dir_ui->ui_mutex); |
| 1185 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1186 | insert_inode_hash(inode); |
| 1187 | d_instantiate(dentry, inode); |
Eric Biggers | 6b46d44 | 2018-01-11 23:27:00 -0500 | [diff] [blame] | 1188 | err = 0; |
| 1189 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1190 | |
| 1191 | out_cancel: |
| 1192 | dir->i_size -= sz_change; |
| 1193 | dir_ui->ui_size = dir->i_size; |
| 1194 | mutex_unlock(&dir_ui->ui_mutex); |
| 1195 | out_inode: |
| 1196 | make_bad_inode(inode); |
| 1197 | iput(inode); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1198 | out_fname: |
| 1199 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1200 | out_budg: |
| 1201 | ubifs_release_budget(c, &req); |
| 1202 | return err; |
| 1203 | } |
| 1204 | |
| 1205 | /** |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1206 | * lock_4_inodes - a wrapper for locking three UBIFS inodes. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1207 | * @inode1: first inode |
| 1208 | * @inode2: second inode |
| 1209 | * @inode3: third inode |
Alexander Dahl | 7296c8a | 2021-09-20 09:05:55 +0200 | [diff] [blame] | 1210 | * @inode4: fourth inode |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1211 | * |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1212 | * This function is used for 'ubifs_rename()' and @inode1 may be the same as |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1213 | * @inode2 whereas @inode3 and @inode4 may be %NULL. |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1214 | * |
| 1215 | * We do not implement any tricks to guarantee strict lock ordering, because |
| 1216 | * VFS has already done it for us on the @i_mutex. So this is just a simple |
| 1217 | * wrapper function. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1218 | */ |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1219 | static void lock_4_inodes(struct inode *inode1, struct inode *inode2, |
| 1220 | struct inode *inode3, struct inode *inode4) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1221 | { |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1222 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
| 1223 | if (inode2 != inode1) |
| 1224 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); |
| 1225 | if (inode3) |
| 1226 | mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1227 | if (inode4) |
| 1228 | mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /** |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1232 | * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1233 | * @inode1: first inode |
| 1234 | * @inode2: second inode |
| 1235 | * @inode3: third inode |
Alexander Dahl | 7296c8a | 2021-09-20 09:05:55 +0200 | [diff] [blame] | 1236 | * @inode4: fourth inode |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1237 | */ |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1238 | static void unlock_4_inodes(struct inode *inode1, struct inode *inode2, |
| 1239 | struct inode *inode3, struct inode *inode4) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1240 | { |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1241 | if (inode4) |
| 1242 | mutex_unlock(&ubifs_inode(inode4)->ui_mutex); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1243 | if (inode3) |
| 1244 | mutex_unlock(&ubifs_inode(inode3)->ui_mutex); |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1245 | if (inode1 != inode2) |
| 1246 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
| 1247 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1248 | } |
| 1249 | |
Richard Weinberger | 390975a | 2016-10-18 21:21:43 +0200 | [diff] [blame] | 1250 | static int do_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1251 | struct inode *new_dir, struct dentry *new_dentry, |
| 1252 | unsigned int flags) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1253 | { |
| 1254 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1255 | struct inode *old_inode = d_inode(old_dentry); |
| 1256 | struct inode *new_inode = d_inode(new_dentry); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1257 | struct inode *whiteout = NULL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1258 | struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1259 | struct ubifs_inode *whiteout_ui = NULL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1260 | int err, release, sync = 0, move = (new_dir != old_dir); |
| 1261 | int is_dir = S_ISDIR(old_inode->i_mode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1262 | int unlink = !!new_inode, new_sz, old_sz; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1263 | struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, |
| 1264 | .dirtied_ino = 3 }; |
| 1265 | struct ubifs_budget_req ino_req = { .dirtied_ino = 1, |
Artem Bityutskiy | dab4b4d | 2008-07-24 14:52:45 +0300 | [diff] [blame] | 1266 | .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) }; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 1267 | struct timespec64 time; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 1268 | unsigned int saved_nlink; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1269 | struct fscrypt_name old_nm, new_nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1270 | |
| 1271 | /* |
| 1272 | * Budget request settings: deletion direntry, new direntry, removing |
| 1273 | * the old inode, and changing old and new parent directory inodes. |
| 1274 | * |
| 1275 | * However, this operation also marks the target inode as dirty and |
| 1276 | * does not write it, so we allocate budget for the target inode |
| 1277 | * separately. |
| 1278 | */ |
| 1279 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1280 | dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x", |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 1281 | old_dentry, old_inode->i_ino, old_dir->i_ino, |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1282 | new_dentry, new_dir->i_ino, flags); |
| 1283 | |
Richard Weinberger | 9ca2d73 | 2019-04-05 00:34:38 +0200 | [diff] [blame] | 1284 | if (unlink) { |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 1285 | ubifs_assert(c, inode_is_locked(new_inode)); |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1286 | |
Richard Weinberger | 9ca2d73 | 2019-04-05 00:34:38 +0200 | [diff] [blame] | 1287 | err = ubifs_purge_xattrs(new_inode); |
| 1288 | if (err) |
| 1289 | return err; |
| 1290 | } |
| 1291 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1292 | if (unlink && is_dir) { |
Richard Weinberger | f6337d8 | 2016-09-19 20:54:19 +0200 | [diff] [blame] | 1293 | err = ubifs_check_dir_empty(new_inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1294 | if (err) |
| 1295 | return err; |
| 1296 | } |
| 1297 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1298 | err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1299 | if (err) |
| 1300 | return err; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1301 | |
| 1302 | err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm); |
| 1303 | if (err) { |
| 1304 | fscrypt_free_filename(&old_nm); |
| 1305 | return err; |
| 1306 | } |
| 1307 | |
| 1308 | new_sz = CALC_DENT_SIZE(fname_len(&new_nm)); |
| 1309 | old_sz = CALC_DENT_SIZE(fname_len(&old_nm)); |
| 1310 | |
| 1311 | err = ubifs_budget_space(c, &req); |
| 1312 | if (err) { |
| 1313 | fscrypt_free_filename(&old_nm); |
| 1314 | fscrypt_free_filename(&new_nm); |
| 1315 | return err; |
| 1316 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1317 | err = ubifs_budget_space(c, &ino_req); |
| 1318 | if (err) { |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1319 | fscrypt_free_filename(&old_nm); |
| 1320 | fscrypt_free_filename(&new_nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1321 | ubifs_release_budget(c, &req); |
| 1322 | return err; |
| 1323 | } |
| 1324 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1325 | if (flags & RENAME_WHITEOUT) { |
| 1326 | union ubifs_dev_desc *dev = NULL; |
| 1327 | |
| 1328 | dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); |
| 1329 | if (!dev) { |
Hyunchul Lee | bb50c63 | 2017-05-17 08:58:02 +0900 | [diff] [blame] | 1330 | err = -ENOMEM; |
| 1331 | goto out_release; |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout); |
| 1335 | if (err) { |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1336 | kfree(dev); |
Hyunchul Lee | bb50c63 | 2017-05-17 08:58:02 +0900 | [diff] [blame] | 1337 | goto out_release; |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
Zhihao Cheng | a801fcf | 2021-06-18 16:11:03 +0800 | [diff] [blame] | 1340 | spin_lock(&whiteout->i_lock); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1341 | whiteout->i_state |= I_LINKABLE; |
Zhihao Cheng | a801fcf | 2021-06-18 16:11:03 +0800 | [diff] [blame] | 1342 | spin_unlock(&whiteout->i_lock); |
| 1343 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1344 | whiteout_ui = ubifs_inode(whiteout); |
| 1345 | whiteout_ui->data = dev; |
| 1346 | whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0)); |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 1347 | ubifs_assert(c, !whiteout_ui->dirty); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | lock_4_inodes(old_dir, new_dir, new_inode, whiteout); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1351 | |
| 1352 | /* |
| 1353 | * Like most other Unix systems, set the @i_ctime for inodes on a |
| 1354 | * rename. |
| 1355 | */ |
Deepa Dinamani | 607a11a | 2017-05-08 15:59:25 -0700 | [diff] [blame] | 1356 | time = current_time(old_dir); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1357 | old_inode->i_ctime = time; |
| 1358 | |
| 1359 | /* We must adjust parent link count when renaming directories */ |
| 1360 | if (is_dir) { |
| 1361 | if (move) { |
| 1362 | /* |
| 1363 | * @old_dir loses a link because we are moving |
| 1364 | * @old_inode to a different directory. |
| 1365 | */ |
| 1366 | drop_nlink(old_dir); |
| 1367 | /* |
| 1368 | * @new_dir only gains a link if we are not also |
| 1369 | * overwriting an existing directory. |
| 1370 | */ |
| 1371 | if (!unlink) |
| 1372 | inc_nlink(new_dir); |
| 1373 | } else { |
| 1374 | /* |
| 1375 | * @old_inode is not moving to a different directory, |
| 1376 | * but @old_dir still loses a link if we are |
| 1377 | * overwriting an existing directory. |
| 1378 | */ |
| 1379 | if (unlink) |
| 1380 | drop_nlink(old_dir); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | old_dir->i_size -= old_sz; |
| 1385 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; |
| 1386 | old_dir->i_mtime = old_dir->i_ctime = time; |
| 1387 | new_dir->i_mtime = new_dir->i_ctime = time; |
| 1388 | |
| 1389 | /* |
| 1390 | * And finally, if we unlinked a direntry which happened to have the |
| 1391 | * same name as the moved direntry, we have to decrement @i_nlink of |
| 1392 | * the unlinked inode and change its ctime. |
| 1393 | */ |
| 1394 | if (unlink) { |
| 1395 | /* |
| 1396 | * Directories cannot have hard-links, so if this is a |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 1397 | * directory, just clear @i_nlink. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1398 | */ |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 1399 | saved_nlink = new_inode->i_nlink; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1400 | if (is_dir) |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 1401 | clear_nlink(new_inode); |
| 1402 | else |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1403 | drop_nlink(new_inode); |
| 1404 | new_inode->i_ctime = time; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1405 | } else { |
| 1406 | new_dir->i_size += new_sz; |
| 1407 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; |
| 1408 | } |
| 1409 | |
| 1410 | /* |
| 1411 | * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode |
| 1412 | * is dirty, because this will be done later on at the end of |
| 1413 | * 'ubifs_rename()'. |
| 1414 | */ |
| 1415 | if (IS_SYNC(old_inode)) { |
| 1416 | sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir); |
| 1417 | if (unlink && IS_SYNC(new_inode)) |
| 1418 | sync = 1; |
| 1419 | } |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1420 | |
| 1421 | if (whiteout) { |
| 1422 | struct ubifs_budget_req wht_req = { .dirtied_ino = 1, |
| 1423 | .dirtied_ino_d = \ |
| 1424 | ALIGN(ubifs_inode(whiteout)->data_len, 8) }; |
| 1425 | |
| 1426 | err = ubifs_budget_space(c, &wht_req); |
| 1427 | if (err) { |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1428 | kfree(whiteout_ui->data); |
| 1429 | whiteout_ui->data_len = 0; |
| 1430 | iput(whiteout); |
Hyunchul Lee | bb50c63 | 2017-05-17 08:58:02 +0900 | [diff] [blame] | 1431 | goto out_release; |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | inc_nlink(whiteout); |
| 1435 | mark_inode_dirty(whiteout); |
Zhihao Cheng | a801fcf | 2021-06-18 16:11:03 +0800 | [diff] [blame] | 1436 | |
| 1437 | spin_lock(&whiteout->i_lock); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1438 | whiteout->i_state &= ~I_LINKABLE; |
Zhihao Cheng | a801fcf | 2021-06-18 16:11:03 +0800 | [diff] [blame] | 1439 | spin_unlock(&whiteout->i_lock); |
| 1440 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1441 | iput(whiteout); |
| 1442 | } |
| 1443 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1444 | err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir, |
| 1445 | new_inode, &new_nm, whiteout, sync); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1446 | if (err) |
| 1447 | goto out_cancel; |
| 1448 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1449 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1450 | ubifs_release_budget(c, &req); |
| 1451 | |
| 1452 | mutex_lock(&old_inode_ui->ui_mutex); |
| 1453 | release = old_inode_ui->dirty; |
| 1454 | mark_inode_dirty_sync(old_inode); |
| 1455 | mutex_unlock(&old_inode_ui->ui_mutex); |
| 1456 | |
| 1457 | if (release) |
| 1458 | ubifs_release_budget(c, &ino_req); |
| 1459 | if (IS_SYNC(old_inode)) |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 1460 | err = old_inode->i_sb->s_op->write_inode(old_inode, NULL); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1461 | |
| 1462 | fscrypt_free_filename(&old_nm); |
| 1463 | fscrypt_free_filename(&new_nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1464 | return err; |
| 1465 | |
| 1466 | out_cancel: |
| 1467 | if (unlink) { |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 1468 | set_nlink(new_inode, saved_nlink); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1469 | } else { |
| 1470 | new_dir->i_size -= new_sz; |
| 1471 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; |
| 1472 | } |
| 1473 | old_dir->i_size += old_sz; |
| 1474 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; |
| 1475 | if (is_dir) { |
| 1476 | if (move) { |
| 1477 | inc_nlink(old_dir); |
| 1478 | if (!unlink) |
| 1479 | drop_nlink(new_dir); |
| 1480 | } else { |
| 1481 | if (unlink) |
| 1482 | inc_nlink(old_dir); |
| 1483 | } |
| 1484 | } |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1485 | if (whiteout) { |
| 1486 | drop_nlink(whiteout); |
| 1487 | iput(whiteout); |
| 1488 | } |
| 1489 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); |
Hyunchul Lee | bb50c63 | 2017-05-17 08:58:02 +0900 | [diff] [blame] | 1490 | out_release: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1491 | ubifs_release_budget(c, &ino_req); |
| 1492 | ubifs_release_budget(c, &req); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1493 | fscrypt_free_filename(&old_nm); |
| 1494 | fscrypt_free_filename(&new_nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1495 | return err; |
| 1496 | } |
| 1497 | |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1498 | static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry, |
| 1499 | struct inode *new_dir, struct dentry *new_dentry) |
| 1500 | { |
| 1501 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; |
| 1502 | struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, |
| 1503 | .dirtied_ino = 2 }; |
| 1504 | int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir); |
| 1505 | struct inode *fst_inode = d_inode(old_dentry); |
| 1506 | struct inode *snd_inode = d_inode(new_dentry); |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 1507 | struct timespec64 time; |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1508 | int err; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1509 | struct fscrypt_name fst_nm, snd_nm; |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1510 | |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 1511 | ubifs_assert(c, fst_inode && snd_inode); |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1512 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1513 | err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm); |
| 1514 | if (err) |
| 1515 | return err; |
| 1516 | |
| 1517 | err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm); |
| 1518 | if (err) { |
| 1519 | fscrypt_free_filename(&fst_nm); |
| 1520 | return err; |
| 1521 | } |
| 1522 | |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1523 | lock_4_inodes(old_dir, new_dir, NULL, NULL); |
| 1524 | |
Deepa Dinamani | 607a11a | 2017-05-08 15:59:25 -0700 | [diff] [blame] | 1525 | time = current_time(old_dir); |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1526 | fst_inode->i_ctime = time; |
| 1527 | snd_inode->i_ctime = time; |
| 1528 | old_dir->i_mtime = old_dir->i_ctime = time; |
| 1529 | new_dir->i_mtime = new_dir->i_ctime = time; |
| 1530 | |
| 1531 | if (old_dir != new_dir) { |
| 1532 | if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) { |
| 1533 | inc_nlink(new_dir); |
| 1534 | drop_nlink(old_dir); |
| 1535 | } |
| 1536 | else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) { |
| 1537 | drop_nlink(new_dir); |
| 1538 | inc_nlink(old_dir); |
| 1539 | } |
| 1540 | } |
| 1541 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1542 | err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir, |
| 1543 | snd_inode, &snd_nm, sync); |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1544 | |
| 1545 | unlock_4_inodes(old_dir, new_dir, NULL, NULL); |
| 1546 | ubifs_release_budget(c, &req); |
| 1547 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1548 | fscrypt_free_filename(&fst_nm); |
| 1549 | fscrypt_free_filename(&snd_nm); |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1550 | return err; |
| 1551 | } |
| 1552 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 1553 | static int ubifs_rename(struct user_namespace *mnt_userns, |
| 1554 | struct inode *old_dir, struct dentry *old_dentry, |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1555 | struct inode *new_dir, struct dentry *new_dentry, |
| 1556 | unsigned int flags) |
| 1557 | { |
Eric Biggers | 0c1ad52 | 2017-11-29 12:43:15 -0800 | [diff] [blame] | 1558 | int err; |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 1559 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; |
Eric Biggers | 0c1ad52 | 2017-11-29 12:43:15 -0800 | [diff] [blame] | 1560 | |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1561 | if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE)) |
| 1562 | return -EINVAL; |
| 1563 | |
Richard Weinberger | 6eb61d5 | 2018-07-12 13:01:57 +0200 | [diff] [blame] | 1564 | ubifs_assert(c, inode_is_locked(old_dir)); |
| 1565 | ubifs_assert(c, inode_is_locked(new_dir)); |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1566 | |
Eric Biggers | 0c1ad52 | 2017-11-29 12:43:15 -0800 | [diff] [blame] | 1567 | err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, |
| 1568 | flags); |
| 1569 | if (err) |
| 1570 | return err; |
| 1571 | |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1572 | if (flags & RENAME_EXCHANGE) |
| 1573 | return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry); |
| 1574 | |
Richard Weinberger | 390975a | 2016-10-18 21:21:43 +0200 | [diff] [blame] | 1575 | return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1576 | } |
| 1577 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 1578 | int ubifs_getattr(struct user_namespace *mnt_userns, const struct path *path, |
| 1579 | struct kstat *stat, u32 request_mask, unsigned int flags) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1580 | { |
| 1581 | loff_t size; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 1582 | struct inode *inode = d_inode(path->dentry); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1583 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 1584 | |
| 1585 | mutex_lock(&ui->ui_mutex); |
Richard Weinberger | a02a6eb | 2017-05-21 00:16:26 +0200 | [diff] [blame] | 1586 | |
| 1587 | if (ui->flags & UBIFS_APPEND_FL) |
| 1588 | stat->attributes |= STATX_ATTR_APPEND; |
| 1589 | if (ui->flags & UBIFS_COMPR_FL) |
| 1590 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 1591 | if (ui->flags & UBIFS_CRYPT_FL) |
| 1592 | stat->attributes |= STATX_ATTR_ENCRYPTED; |
| 1593 | if (ui->flags & UBIFS_IMMUTABLE_FL) |
| 1594 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 1595 | |
| 1596 | stat->attributes_mask |= (STATX_ATTR_APPEND | |
| 1597 | STATX_ATTR_COMPRESSED | |
| 1598 | STATX_ATTR_ENCRYPTED | |
| 1599 | STATX_ATTR_IMMUTABLE); |
| 1600 | |
Christian Brauner | 0d56a45 | 2021-01-21 14:19:30 +0100 | [diff] [blame] | 1601 | generic_fillattr(&init_user_ns, inode, stat); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1602 | stat->blksize = UBIFS_BLOCK_SIZE; |
| 1603 | stat->size = ui->ui_size; |
| 1604 | |
| 1605 | /* |
| 1606 | * Unfortunately, the 'stat()' system call was designed for block |
| 1607 | * device based file systems, and it is not appropriate for UBIFS, |
| 1608 | * because UBIFS does not have notion of "block". For example, it is |
| 1609 | * difficult to tell how many block a directory takes - it actually |
| 1610 | * takes less than 300 bytes, but we have to round it to block size, |
| 1611 | * which introduces large mistake. This makes utilities like 'du' to |
| 1612 | * report completely senseless numbers. This is the reason why UBIFS |
| 1613 | * goes the same way as JFFS2 - it reports zero blocks for everything |
| 1614 | * but regular files, which makes more sense than reporting completely |
| 1615 | * wrong sizes. |
| 1616 | */ |
| 1617 | if (S_ISREG(inode->i_mode)) { |
| 1618 | size = ui->xattr_size; |
| 1619 | size += stat->size; |
| 1620 | size = ALIGN(size, UBIFS_BLOCK_SIZE); |
| 1621 | /* |
| 1622 | * Note, user-space expects 512-byte blocks count irrespectively |
| 1623 | * of what was reported in @stat->size. |
| 1624 | */ |
| 1625 | stat->blocks = size >> 9; |
| 1626 | } else |
| 1627 | stat->blocks = 0; |
| 1628 | mutex_unlock(&ui->ui_mutex); |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
Artem Bityutskiy | e8b8156 | 2009-01-15 17:43:23 +0200 | [diff] [blame] | 1632 | const struct inode_operations ubifs_dir_inode_operations = { |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1633 | .lookup = ubifs_lookup, |
| 1634 | .create = ubifs_create, |
| 1635 | .link = ubifs_link, |
| 1636 | .symlink = ubifs_symlink, |
| 1637 | .unlink = ubifs_unlink, |
| 1638 | .mkdir = ubifs_mkdir, |
| 1639 | .rmdir = ubifs_rmdir, |
| 1640 | .mknod = ubifs_mknod, |
Richard Weinberger | 390975a | 2016-10-18 21:21:43 +0200 | [diff] [blame] | 1641 | .rename = ubifs_rename, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1642 | .setattr = ubifs_setattr, |
| 1643 | .getattr = ubifs_getattr, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1644 | .listxattr = ubifs_listxattr, |
Dongsheng Yang | 8c1c5f2 | 2015-11-07 12:46:11 +0800 | [diff] [blame] | 1645 | .update_time = ubifs_update_time, |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 1646 | .tmpfile = ubifs_tmpfile, |
Miklos Szeredi | 8871d84 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 1647 | .fileattr_get = ubifs_fileattr_get, |
| 1648 | .fileattr_set = ubifs_fileattr_set, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1649 | }; |
| 1650 | |
Artem Bityutskiy | e8b8156 | 2009-01-15 17:43:23 +0200 | [diff] [blame] | 1651 | const struct file_operations ubifs_dir_operations = { |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 1652 | .llseek = generic_file_llseek, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1653 | .release = ubifs_dir_release, |
| 1654 | .read = generic_read_dir, |
Al Viro | c51da20 | 2016-04-30 22:37:34 -0400 | [diff] [blame] | 1655 | .iterate_shared = ubifs_readdir, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1656 | .fsync = ubifs_fsync, |
| 1657 | .unlocked_ioctl = ubifs_ioctl, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1658 | #ifdef CONFIG_COMPAT |
| 1659 | .compat_ioctl = ubifs_compat_ioctl, |
| 1660 | #endif |
| 1661 | }; |