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