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