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