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) { |
| 609 | dbg_gen("feed '%s', ino %llu, new f_pos %#x", |
Alexander Beregalov | 7424bac | 2008-09-17 22:09:41 +0400 | [diff] [blame] | 610 | dent->name, (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); |
| 751 | inc_nlink(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 752 | ihold(inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 753 | inode->i_ctime = ubifs_current_time(inode); |
| 754 | dir->i_size += sz_change; |
| 755 | dir_ui->ui_size = dir->i_size; |
| 756 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 757 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 758 | if (err) |
| 759 | goto out_cancel; |
| 760 | unlock_2_inodes(dir, inode); |
| 761 | |
| 762 | ubifs_release_budget(c, &req); |
| 763 | d_instantiate(dentry, inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 764 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 765 | return 0; |
| 766 | |
| 767 | out_cancel: |
| 768 | dir->i_size -= sz_change; |
| 769 | dir_ui->ui_size = dir->i_size; |
| 770 | drop_nlink(inode); |
| 771 | unlock_2_inodes(dir, inode); |
| 772 | ubifs_release_budget(c, &req); |
| 773 | iput(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 774 | out_fname: |
| 775 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 776 | return err; |
| 777 | } |
| 778 | |
| 779 | static int ubifs_unlink(struct inode *dir, struct dentry *dentry) |
| 780 | { |
| 781 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 782 | struct inode *inode = d_inode(dentry); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 783 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 784 | int err, sz_change, budgeted = 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 785 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 786 | unsigned int saved_nlink = inode->i_nlink; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 787 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 788 | |
| 789 | /* |
| 790 | * Budget request settings: deletion direntry, deletion inode (+1 for |
| 791 | * @dirtied_ino), changing the parent directory inode. If budgeting |
| 792 | * fails, go ahead anyway because we have extra space reserved for |
| 793 | * deletions. |
| 794 | */ |
| 795 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 796 | dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu", |
| 797 | dentry, inode->i_ino, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 798 | inode->i_nlink, dir->i_ino); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 799 | |
| 800 | if (ubifs_crypt_is_encrypted(dir)) { |
| 801 | err = fscrypt_get_encryption_info(dir); |
| 802 | if (err && err != -ENOKEY) |
| 803 | return err; |
| 804 | } |
| 805 | |
| 806 | err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm); |
| 807 | if (err) |
| 808 | return err; |
| 809 | |
| 810 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 811 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 812 | ubifs_assert(inode_is_locked(dir)); |
| 813 | ubifs_assert(inode_is_locked(inode)); |
Artem Bityutskiy | d808efb | 2011-05-31 18:14:38 +0300 | [diff] [blame] | 814 | err = dbg_check_synced_i_size(c, inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 815 | if (err) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 816 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 817 | |
| 818 | err = ubifs_budget_space(c, &req); |
| 819 | if (err) { |
| 820 | if (err != -ENOSPC) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 821 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 822 | budgeted = 0; |
| 823 | } |
| 824 | |
| 825 | lock_2_inodes(dir, inode); |
| 826 | inode->i_ctime = ubifs_current_time(dir); |
| 827 | drop_nlink(inode); |
| 828 | dir->i_size -= sz_change; |
| 829 | dir_ui->ui_size = dir->i_size; |
| 830 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 831 | err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 832 | if (err) |
| 833 | goto out_cancel; |
| 834 | unlock_2_inodes(dir, inode); |
| 835 | |
| 836 | if (budgeted) |
| 837 | ubifs_release_budget(c, &req); |
| 838 | else { |
| 839 | /* We've deleted something - clean the "no space" flags */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 840 | c->bi.nospace = c->bi.nospace_rp = 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 841 | smp_wmb(); |
| 842 | } |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 843 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 844 | return 0; |
| 845 | |
| 846 | out_cancel: |
| 847 | dir->i_size += sz_change; |
| 848 | dir_ui->ui_size = dir->i_size; |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 849 | set_nlink(inode, saved_nlink); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 850 | unlock_2_inodes(dir, inode); |
| 851 | if (budgeted) |
| 852 | ubifs_release_budget(c, &req); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 853 | out_fname: |
| 854 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 855 | return err; |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * check_dir_empty - check if a directory is empty or not. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 860 | * @dir: VFS inode object of the directory to check |
| 861 | * |
| 862 | * This function checks if directory @dir is empty. Returns zero if the |
| 863 | * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes |
| 864 | * in case of of errors. |
| 865 | */ |
Richard Weinberger | f6337d8 | 2016-09-19 20:54:19 +0200 | [diff] [blame] | 866 | int ubifs_check_dir_empty(struct inode *dir) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 867 | { |
Richard Weinberger | f6337d8 | 2016-09-19 20:54:19 +0200 | [diff] [blame] | 868 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 869 | struct fscrypt_name nm = { 0 }; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 870 | struct ubifs_dent_node *dent; |
| 871 | union ubifs_key key; |
| 872 | int err; |
| 873 | |
| 874 | lowest_dent_key(c, &key, dir->i_ino); |
| 875 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
| 876 | if (IS_ERR(dent)) { |
| 877 | err = PTR_ERR(dent); |
| 878 | if (err == -ENOENT) |
| 879 | err = 0; |
| 880 | } else { |
| 881 | kfree(dent); |
| 882 | err = -ENOTEMPTY; |
| 883 | } |
| 884 | return err; |
| 885 | } |
| 886 | |
| 887 | static int ubifs_rmdir(struct inode *dir, struct dentry *dentry) |
| 888 | { |
| 889 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 890 | struct inode *inode = d_inode(dentry); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 891 | int err, sz_change, budgeted = 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 892 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
| 893 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 894 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 895 | |
| 896 | /* |
| 897 | * Budget request settings: deletion direntry, deletion inode and |
| 898 | * changing the parent inode. If budgeting fails, go ahead anyway |
| 899 | * because we have extra space reserved for deletions. |
| 900 | */ |
| 901 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 902 | dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry, |
| 903 | inode->i_ino, dir->i_ino); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 904 | ubifs_assert(inode_is_locked(dir)); |
| 905 | ubifs_assert(inode_is_locked(inode)); |
Richard Weinberger | f6337d8 | 2016-09-19 20:54:19 +0200 | [diff] [blame] | 906 | err = ubifs_check_dir_empty(d_inode(dentry)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 907 | if (err) |
| 908 | return err; |
| 909 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 910 | if (ubifs_crypt_is_encrypted(dir)) { |
| 911 | err = fscrypt_get_encryption_info(dir); |
| 912 | if (err && err != -ENOKEY) |
| 913 | return err; |
| 914 | } |
| 915 | |
| 916 | err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm); |
| 917 | if (err) |
| 918 | return err; |
| 919 | |
| 920 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 921 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 922 | err = ubifs_budget_space(c, &req); |
| 923 | if (err) { |
| 924 | if (err != -ENOSPC) |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 925 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 926 | budgeted = 0; |
| 927 | } |
| 928 | |
| 929 | lock_2_inodes(dir, inode); |
| 930 | inode->i_ctime = ubifs_current_time(dir); |
| 931 | clear_nlink(inode); |
| 932 | drop_nlink(dir); |
| 933 | dir->i_size -= sz_change; |
| 934 | dir_ui->ui_size = dir->i_size; |
| 935 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 936 | err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 937 | if (err) |
| 938 | goto out_cancel; |
| 939 | unlock_2_inodes(dir, inode); |
| 940 | |
| 941 | if (budgeted) |
| 942 | ubifs_release_budget(c, &req); |
| 943 | else { |
| 944 | /* We've deleted something - clean the "no space" flags */ |
Artem Bityutskiy | b137545 | 2011-03-29 18:04:05 +0300 | [diff] [blame] | 945 | c->bi.nospace = c->bi.nospace_rp = 0; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 946 | smp_wmb(); |
| 947 | } |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 948 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 949 | return 0; |
| 950 | |
| 951 | out_cancel: |
| 952 | dir->i_size += sz_change; |
| 953 | dir_ui->ui_size = dir->i_size; |
| 954 | inc_nlink(dir); |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 955 | set_nlink(inode, 2); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 956 | unlock_2_inodes(dir, inode); |
| 957 | if (budgeted) |
| 958 | ubifs_release_budget(c, &req); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 959 | out_fname: |
| 960 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 961 | return err; |
| 962 | } |
| 963 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 964 | 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] | 965 | { |
| 966 | struct inode *inode; |
| 967 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
| 968 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 969 | int err, sz_change; |
Artem Bityutskiy | 182854b | 2008-07-18 18:54:29 +0300 | [diff] [blame] | 970 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 }; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 971 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 972 | |
| 973 | /* |
| 974 | * Budget request settings: new inode, new direntry and changing parent |
| 975 | * directory inode. |
| 976 | */ |
| 977 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 978 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
| 979 | dentry, mode, dir->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 980 | |
| 981 | err = ubifs_budget_space(c, &req); |
| 982 | if (err) |
| 983 | return err; |
| 984 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 985 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm); |
| 986 | if (err) |
| 987 | goto out_budg; |
| 988 | |
| 989 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 990 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 991 | inode = ubifs_new_inode(c, dir, S_IFDIR | mode); |
| 992 | if (IS_ERR(inode)) { |
| 993 | err = PTR_ERR(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 994 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 995 | } |
| 996 | |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 997 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
| 998 | if (err) |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 999 | goto out_inode; |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 1000 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1001 | mutex_lock(&dir_ui->ui_mutex); |
| 1002 | insert_inode_hash(inode); |
| 1003 | inc_nlink(inode); |
| 1004 | inc_nlink(dir); |
| 1005 | dir->i_size += sz_change; |
| 1006 | dir_ui->ui_size = dir->i_size; |
| 1007 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1008 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1009 | if (err) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 1010 | ubifs_err(c, "cannot create directory, error %d", err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1011 | goto out_cancel; |
| 1012 | } |
| 1013 | mutex_unlock(&dir_ui->ui_mutex); |
| 1014 | |
| 1015 | ubifs_release_budget(c, &req); |
| 1016 | d_instantiate(dentry, inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1017 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1018 | return 0; |
| 1019 | |
| 1020 | out_cancel: |
| 1021 | dir->i_size -= sz_change; |
| 1022 | dir_ui->ui_size = dir->i_size; |
| 1023 | drop_nlink(dir); |
| 1024 | mutex_unlock(&dir_ui->ui_mutex); |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 1025 | out_inode: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1026 | make_bad_inode(inode); |
| 1027 | iput(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1028 | out_fname: |
| 1029 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1030 | out_budg: |
| 1031 | ubifs_release_budget(c, &req); |
| 1032 | return err; |
| 1033 | } |
| 1034 | |
| 1035 | static int ubifs_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 1036 | umode_t mode, dev_t rdev) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1037 | { |
| 1038 | struct inode *inode; |
| 1039 | struct ubifs_inode *ui; |
| 1040 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
| 1041 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
| 1042 | union ubifs_dev_desc *dev = NULL; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1043 | int sz_change; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1044 | int err, devlen = 0; |
| 1045 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, |
Artem Bityutskiy | dab4b4d | 2008-07-24 14:52:45 +0300 | [diff] [blame] | 1046 | .new_ino_d = ALIGN(devlen, 8), |
| 1047 | .dirtied_ino = 1 }; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1048 | struct fscrypt_name nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1049 | |
| 1050 | /* |
| 1051 | * Budget request settings: new inode, new direntry and changing parent |
| 1052 | * directory inode. |
| 1053 | */ |
| 1054 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 1055 | dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1056 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1057 | if (S_ISBLK(mode) || S_ISCHR(mode)) { |
| 1058 | dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); |
| 1059 | if (!dev) |
| 1060 | return -ENOMEM; |
| 1061 | devlen = ubifs_encode_dev(dev, rdev); |
| 1062 | } |
| 1063 | |
| 1064 | err = ubifs_budget_space(c, &req); |
| 1065 | if (err) { |
| 1066 | kfree(dev); |
| 1067 | return err; |
| 1068 | } |
| 1069 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1070 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm); |
Richard Weinberger | 63ed657 | 2017-02-10 17:46:01 +0100 | [diff] [blame^] | 1071 | if (err) { |
| 1072 | kfree(dev); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1073 | goto out_budg; |
Richard Weinberger | 63ed657 | 2017-02-10 17:46:01 +0100 | [diff] [blame^] | 1074 | } |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1075 | |
| 1076 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
| 1077 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1078 | inode = ubifs_new_inode(c, dir, mode); |
| 1079 | if (IS_ERR(inode)) { |
| 1080 | kfree(dev); |
| 1081 | err = PTR_ERR(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1082 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | init_special_inode(inode, inode->i_mode, rdev); |
| 1086 | inode->i_size = ubifs_inode(inode)->ui_size = devlen; |
| 1087 | ui = ubifs_inode(inode); |
| 1088 | ui->data = dev; |
| 1089 | ui->data_len = devlen; |
| 1090 | |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 1091 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
| 1092 | if (err) |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 1093 | goto out_inode; |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 1094 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1095 | mutex_lock(&dir_ui->ui_mutex); |
| 1096 | dir->i_size += sz_change; |
| 1097 | dir_ui->ui_size = dir->i_size; |
| 1098 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1099 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1100 | if (err) |
| 1101 | goto out_cancel; |
| 1102 | mutex_unlock(&dir_ui->ui_mutex); |
| 1103 | |
| 1104 | ubifs_release_budget(c, &req); |
| 1105 | insert_inode_hash(inode); |
| 1106 | d_instantiate(dentry, inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1107 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1108 | return 0; |
| 1109 | |
| 1110 | out_cancel: |
| 1111 | dir->i_size -= sz_change; |
| 1112 | dir_ui->ui_size = dir->i_size; |
| 1113 | mutex_unlock(&dir_ui->ui_mutex); |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 1114 | out_inode: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1115 | make_bad_inode(inode); |
| 1116 | iput(inode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1117 | out_fname: |
| 1118 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1119 | out_budg: |
| 1120 | ubifs_release_budget(c, &req); |
| 1121 | return err; |
| 1122 | } |
| 1123 | |
| 1124 | static int ubifs_symlink(struct inode *dir, struct dentry *dentry, |
| 1125 | const char *symname) |
| 1126 | { |
| 1127 | struct inode *inode; |
| 1128 | struct ubifs_inode *ui; |
| 1129 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
| 1130 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
| 1131 | int err, len = strlen(symname); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1132 | int sz_change = CALC_DENT_SIZE(len); |
| 1133 | struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1); |
| 1134 | struct fscrypt_symlink_data *sd = NULL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1135 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, |
Artem Bityutskiy | dab4b4d | 2008-07-24 14:52:45 +0300 | [diff] [blame] | 1136 | .new_ino_d = ALIGN(len, 8), |
| 1137 | .dirtied_ino = 1 }; |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1138 | struct fscrypt_name nm; |
| 1139 | |
| 1140 | if (ubifs_crypt_is_encrypted(dir)) { |
| 1141 | err = fscrypt_get_encryption_info(dir); |
| 1142 | if (err) |
| 1143 | goto out_budg; |
| 1144 | |
| 1145 | if (!fscrypt_has_encryption_key(dir)) { |
| 1146 | err = -EPERM; |
| 1147 | goto out_budg; |
| 1148 | } |
| 1149 | |
| 1150 | disk_link.len = (fscrypt_fname_encrypted_size(dir, len) + |
| 1151 | sizeof(struct fscrypt_symlink_data)); |
| 1152 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1153 | |
| 1154 | /* |
| 1155 | * Budget request settings: new inode, new direntry and changing parent |
| 1156 | * directory inode. |
| 1157 | */ |
| 1158 | |
Al Viro | 4cb2a01 | 2013-09-16 10:58:53 -0400 | [diff] [blame] | 1159 | dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry, |
| 1160 | symname, dir->i_ino); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1161 | |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1162 | if (disk_link.len > UBIFS_MAX_INO_DATA) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1163 | return -ENAMETOOLONG; |
| 1164 | |
| 1165 | err = ubifs_budget_space(c, &req); |
| 1166 | if (err) |
| 1167 | return err; |
| 1168 | |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1169 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm); |
| 1170 | if (err) |
| 1171 | goto out_budg; |
| 1172 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1173 | inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO); |
| 1174 | if (IS_ERR(inode)) { |
| 1175 | err = PTR_ERR(inode); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1176 | goto out_fname; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | ui = ubifs_inode(inode); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1180 | ui->data = kmalloc(disk_link.len, GFP_NOFS); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1181 | if (!ui->data) { |
| 1182 | err = -ENOMEM; |
| 1183 | goto out_inode; |
| 1184 | } |
| 1185 | |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1186 | if (ubifs_crypt_is_encrypted(dir)) { |
| 1187 | struct qstr istr = QSTR_INIT(symname, len); |
| 1188 | struct fscrypt_str ostr; |
| 1189 | |
| 1190 | sd = kzalloc(disk_link.len, GFP_NOFS); |
| 1191 | if (!sd) { |
| 1192 | err = -ENOMEM; |
| 1193 | goto out_inode; |
| 1194 | } |
| 1195 | |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1196 | ostr.name = sd->encrypted_path; |
| 1197 | ostr.len = disk_link.len; |
| 1198 | |
| 1199 | err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr); |
| 1200 | if (err) { |
| 1201 | kfree(sd); |
| 1202 | goto out_inode; |
| 1203 | } |
| 1204 | |
| 1205 | sd->len = cpu_to_le16(ostr.len); |
| 1206 | disk_link.name = (char *)sd; |
| 1207 | } else { |
| 1208 | inode->i_link = ui->data; |
| 1209 | } |
| 1210 | |
| 1211 | memcpy(ui->data, disk_link.name, disk_link.len); |
| 1212 | ((char *)ui->data)[disk_link.len - 1] = '\0'; |
| 1213 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1214 | /* |
| 1215 | * The terminating zero byte is not written to the flash media and it |
| 1216 | * is put just to make later in-memory string processing simpler. Thus, |
| 1217 | * data length is @len, not @len + %1. |
| 1218 | */ |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1219 | ui->data_len = disk_link.len - 1; |
| 1220 | inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1221 | |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 1222 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
| 1223 | if (err) |
Taesoo Kim | 9401a79 | 2015-03-25 09:53:37 +0100 | [diff] [blame] | 1224 | goto out_inode; |
Subodh Nijsure | d7f0b70 | 2014-10-31 13:50:30 -0500 | [diff] [blame] | 1225 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1226 | mutex_lock(&dir_ui->ui_mutex); |
| 1227 | dir->i_size += sz_change; |
| 1228 | dir_ui->ui_size = dir->i_size; |
| 1229 | dir->i_mtime = dir->i_ctime = inode->i_ctime; |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1230 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1231 | if (err) |
| 1232 | goto out_cancel; |
| 1233 | mutex_unlock(&dir_ui->ui_mutex); |
| 1234 | |
| 1235 | ubifs_release_budget(c, &req); |
| 1236 | insert_inode_hash(inode); |
| 1237 | d_instantiate(dentry, inode); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1238 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1239 | return 0; |
| 1240 | |
| 1241 | out_cancel: |
| 1242 | dir->i_size -= sz_change; |
| 1243 | dir_ui->ui_size = dir->i_size; |
| 1244 | mutex_unlock(&dir_ui->ui_mutex); |
| 1245 | out_inode: |
| 1246 | make_bad_inode(inode); |
| 1247 | iput(inode); |
Richard Weinberger | ca7f85b | 2016-10-08 13:24:26 +0200 | [diff] [blame] | 1248 | out_fname: |
| 1249 | fscrypt_free_filename(&nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1250 | out_budg: |
| 1251 | ubifs_release_budget(c, &req); |
| 1252 | return err; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1256 | * lock_4_inodes - a wrapper for locking three UBIFS inodes. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1257 | * @inode1: first inode |
| 1258 | * @inode2: second inode |
| 1259 | * @inode3: third inode |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1260 | * @inode4: fouth inode |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1261 | * |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1262 | * 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] | 1263 | * @inode2 whereas @inode3 and @inode4 may be %NULL. |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1264 | * |
| 1265 | * We do not implement any tricks to guarantee strict lock ordering, because |
| 1266 | * VFS has already done it for us on the @i_mutex. So this is just a simple |
| 1267 | * wrapper function. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1268 | */ |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1269 | static void lock_4_inodes(struct inode *inode1, struct inode *inode2, |
| 1270 | struct inode *inode3, struct inode *inode4) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1271 | { |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1272 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
| 1273 | if (inode2 != inode1) |
| 1274 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); |
| 1275 | if (inode3) |
| 1276 | mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1277 | if (inode4) |
| 1278 | mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /** |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1282 | * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1283 | * @inode1: first inode |
| 1284 | * @inode2: second inode |
| 1285 | * @inode3: third inode |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1286 | * @inode4: fouth inode |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1287 | */ |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1288 | static void unlock_4_inodes(struct inode *inode1, struct inode *inode2, |
| 1289 | struct inode *inode3, struct inode *inode4) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1290 | { |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1291 | if (inode4) |
| 1292 | mutex_unlock(&ubifs_inode(inode4)->ui_mutex); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1293 | if (inode3) |
| 1294 | mutex_unlock(&ubifs_inode(inode3)->ui_mutex); |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1295 | if (inode1 != inode2) |
| 1296 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
| 1297 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1298 | } |
| 1299 | |
Richard Weinberger | 390975a | 2016-10-18 21:21:43 +0200 | [diff] [blame] | 1300 | static int do_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 1301 | struct inode *new_dir, struct dentry *new_dentry, |
| 1302 | unsigned int flags) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1303 | { |
| 1304 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1305 | struct inode *old_inode = d_inode(old_dentry); |
| 1306 | struct inode *new_inode = d_inode(new_dentry); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1307 | struct inode *whiteout = NULL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1308 | struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode); |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1309 | struct ubifs_inode *whiteout_ui = NULL; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1310 | int err, release, sync = 0, move = (new_dir != old_dir); |
| 1311 | int is_dir = S_ISDIR(old_inode->i_mode); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1312 | int unlink = !!new_inode, new_sz, old_sz; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1313 | struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, |
| 1314 | .dirtied_ino = 3 }; |
| 1315 | struct ubifs_budget_req ino_req = { .dirtied_ino = 1, |
Artem Bityutskiy | dab4b4d | 2008-07-24 14:52:45 +0300 | [diff] [blame] | 1316 | .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) }; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1317 | struct timespec time; |
Alexandre Pereira da Silva | 782759b | 2012-06-25 17:47:49 -0300 | [diff] [blame] | 1318 | unsigned int uninitialized_var(saved_nlink); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1319 | struct fscrypt_name old_nm, new_nm; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1320 | |
Miklos Szeredi | f03b8ad | 2016-09-27 11:03:57 +0200 | [diff] [blame] | 1321 | if (flags & ~RENAME_NOREPLACE) |
| 1322 | return -EINVAL; |
| 1323 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1324 | /* |
| 1325 | * Budget request settings: deletion direntry, new direntry, removing |
| 1326 | * the old inode, and changing old and new parent directory inodes. |
| 1327 | * |
| 1328 | * However, this operation also marks the target inode as dirty and |
| 1329 | * does not write it, so we allocate budget for the target inode |
| 1330 | * separately. |
| 1331 | */ |
| 1332 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1333 | 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] | 1334 | old_dentry, old_inode->i_ino, old_dir->i_ino, |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1335 | new_dentry, new_dir->i_ino, flags); |
| 1336 | |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1337 | if (unlink) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1338 | ubifs_assert(inode_is_locked(new_inode)); |
Artem Bityutskiy | 82c1593 | 2009-01-20 16:46:02 +0200 | [diff] [blame] | 1339 | |
Richard Weinberger | ac7e47a | 2016-09-29 20:00:38 +0200 | [diff] [blame] | 1340 | if (old_dir != new_dir) { |
| 1341 | if (ubifs_crypt_is_encrypted(new_dir) && |
| 1342 | !fscrypt_has_permitted_context(new_dir, old_inode)) |
| 1343 | return -EPERM; |
| 1344 | } |
| 1345 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1346 | if (unlink && is_dir) { |
Richard Weinberger | f6337d8 | 2016-09-19 20:54:19 +0200 | [diff] [blame] | 1347 | err = ubifs_check_dir_empty(new_inode); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1348 | if (err) |
| 1349 | return err; |
| 1350 | } |
| 1351 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1352 | 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] | 1353 | if (err) |
| 1354 | return err; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1355 | |
| 1356 | err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm); |
| 1357 | if (err) { |
| 1358 | fscrypt_free_filename(&old_nm); |
| 1359 | return err; |
| 1360 | } |
| 1361 | |
| 1362 | new_sz = CALC_DENT_SIZE(fname_len(&new_nm)); |
| 1363 | old_sz = CALC_DENT_SIZE(fname_len(&old_nm)); |
| 1364 | |
| 1365 | err = ubifs_budget_space(c, &req); |
| 1366 | if (err) { |
| 1367 | fscrypt_free_filename(&old_nm); |
| 1368 | fscrypt_free_filename(&new_nm); |
| 1369 | return err; |
| 1370 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1371 | err = ubifs_budget_space(c, &ino_req); |
| 1372 | if (err) { |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1373 | fscrypt_free_filename(&old_nm); |
| 1374 | fscrypt_free_filename(&new_nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1375 | ubifs_release_budget(c, &req); |
| 1376 | return err; |
| 1377 | } |
| 1378 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1379 | if (flags & RENAME_WHITEOUT) { |
| 1380 | union ubifs_dev_desc *dev = NULL; |
| 1381 | |
| 1382 | dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); |
| 1383 | if (!dev) { |
| 1384 | ubifs_release_budget(c, &req); |
| 1385 | ubifs_release_budget(c, &ino_req); |
| 1386 | return -ENOMEM; |
| 1387 | } |
| 1388 | |
| 1389 | err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout); |
| 1390 | if (err) { |
| 1391 | ubifs_release_budget(c, &req); |
| 1392 | ubifs_release_budget(c, &ino_req); |
| 1393 | kfree(dev); |
| 1394 | return err; |
| 1395 | } |
| 1396 | |
| 1397 | whiteout->i_state |= I_LINKABLE; |
| 1398 | whiteout_ui = ubifs_inode(whiteout); |
| 1399 | whiteout_ui->data = dev; |
| 1400 | whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0)); |
| 1401 | ubifs_assert(!whiteout_ui->dirty); |
| 1402 | } |
| 1403 | |
| 1404 | lock_4_inodes(old_dir, new_dir, new_inode, whiteout); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1405 | |
| 1406 | /* |
| 1407 | * Like most other Unix systems, set the @i_ctime for inodes on a |
| 1408 | * rename. |
| 1409 | */ |
| 1410 | time = ubifs_current_time(old_dir); |
| 1411 | old_inode->i_ctime = time; |
| 1412 | |
| 1413 | /* We must adjust parent link count when renaming directories */ |
| 1414 | if (is_dir) { |
| 1415 | if (move) { |
| 1416 | /* |
| 1417 | * @old_dir loses a link because we are moving |
| 1418 | * @old_inode to a different directory. |
| 1419 | */ |
| 1420 | drop_nlink(old_dir); |
| 1421 | /* |
| 1422 | * @new_dir only gains a link if we are not also |
| 1423 | * overwriting an existing directory. |
| 1424 | */ |
| 1425 | if (!unlink) |
| 1426 | inc_nlink(new_dir); |
| 1427 | } else { |
| 1428 | /* |
| 1429 | * @old_inode is not moving to a different directory, |
| 1430 | * but @old_dir still loses a link if we are |
| 1431 | * overwriting an existing directory. |
| 1432 | */ |
| 1433 | if (unlink) |
| 1434 | drop_nlink(old_dir); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | old_dir->i_size -= old_sz; |
| 1439 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; |
| 1440 | old_dir->i_mtime = old_dir->i_ctime = time; |
| 1441 | new_dir->i_mtime = new_dir->i_ctime = time; |
| 1442 | |
| 1443 | /* |
| 1444 | * And finally, if we unlinked a direntry which happened to have the |
| 1445 | * same name as the moved direntry, we have to decrement @i_nlink of |
| 1446 | * the unlinked inode and change its ctime. |
| 1447 | */ |
| 1448 | if (unlink) { |
| 1449 | /* |
| 1450 | * Directories cannot have hard-links, so if this is a |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 1451 | * directory, just clear @i_nlink. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1452 | */ |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 1453 | saved_nlink = new_inode->i_nlink; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1454 | if (is_dir) |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 1455 | clear_nlink(new_inode); |
| 1456 | else |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1457 | drop_nlink(new_inode); |
| 1458 | new_inode->i_ctime = time; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1459 | } else { |
| 1460 | new_dir->i_size += new_sz; |
| 1461 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode |
| 1466 | * is dirty, because this will be done later on at the end of |
| 1467 | * 'ubifs_rename()'. |
| 1468 | */ |
| 1469 | if (IS_SYNC(old_inode)) { |
| 1470 | sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir); |
| 1471 | if (unlink && IS_SYNC(new_inode)) |
| 1472 | sync = 1; |
| 1473 | } |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1474 | |
| 1475 | if (whiteout) { |
| 1476 | struct ubifs_budget_req wht_req = { .dirtied_ino = 1, |
| 1477 | .dirtied_ino_d = \ |
| 1478 | ALIGN(ubifs_inode(whiteout)->data_len, 8) }; |
| 1479 | |
| 1480 | err = ubifs_budget_space(c, &wht_req); |
| 1481 | if (err) { |
| 1482 | ubifs_release_budget(c, &req); |
| 1483 | ubifs_release_budget(c, &ino_req); |
| 1484 | kfree(whiteout_ui->data); |
| 1485 | whiteout_ui->data_len = 0; |
| 1486 | iput(whiteout); |
| 1487 | return err; |
| 1488 | } |
| 1489 | |
| 1490 | inc_nlink(whiteout); |
| 1491 | mark_inode_dirty(whiteout); |
| 1492 | whiteout->i_state &= ~I_LINKABLE; |
| 1493 | iput(whiteout); |
| 1494 | } |
| 1495 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1496 | err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir, |
| 1497 | new_inode, &new_nm, whiteout, sync); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1498 | if (err) |
| 1499 | goto out_cancel; |
| 1500 | |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1501 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1502 | ubifs_release_budget(c, &req); |
| 1503 | |
| 1504 | mutex_lock(&old_inode_ui->ui_mutex); |
| 1505 | release = old_inode_ui->dirty; |
| 1506 | mark_inode_dirty_sync(old_inode); |
| 1507 | mutex_unlock(&old_inode_ui->ui_mutex); |
| 1508 | |
| 1509 | if (release) |
| 1510 | ubifs_release_budget(c, &ino_req); |
| 1511 | if (IS_SYNC(old_inode)) |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 1512 | err = old_inode->i_sb->s_op->write_inode(old_inode, NULL); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1513 | |
| 1514 | fscrypt_free_filename(&old_nm); |
| 1515 | fscrypt_free_filename(&new_nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1516 | return err; |
| 1517 | |
| 1518 | out_cancel: |
| 1519 | if (unlink) { |
Artem Bityutskiy | c43be10 | 2012-02-07 10:58:51 +0200 | [diff] [blame] | 1520 | set_nlink(new_inode, saved_nlink); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1521 | } else { |
| 1522 | new_dir->i_size -= new_sz; |
| 1523 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; |
| 1524 | } |
| 1525 | old_dir->i_size += old_sz; |
| 1526 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; |
| 1527 | if (is_dir) { |
| 1528 | if (move) { |
| 1529 | inc_nlink(old_dir); |
| 1530 | if (!unlink) |
| 1531 | drop_nlink(new_dir); |
| 1532 | } else { |
| 1533 | if (unlink) |
| 1534 | inc_nlink(old_dir); |
| 1535 | } |
| 1536 | } |
Richard Weinberger | 9e0a1ff | 2016-09-14 22:28:50 +0200 | [diff] [blame] | 1537 | if (whiteout) { |
| 1538 | drop_nlink(whiteout); |
| 1539 | iput(whiteout); |
| 1540 | } |
| 1541 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1542 | ubifs_release_budget(c, &ino_req); |
| 1543 | ubifs_release_budget(c, &req); |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1544 | fscrypt_free_filename(&old_nm); |
| 1545 | fscrypt_free_filename(&new_nm); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1546 | return err; |
| 1547 | } |
| 1548 | |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1549 | static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry, |
| 1550 | struct inode *new_dir, struct dentry *new_dentry) |
| 1551 | { |
| 1552 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; |
| 1553 | struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, |
| 1554 | .dirtied_ino = 2 }; |
| 1555 | int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir); |
| 1556 | struct inode *fst_inode = d_inode(old_dentry); |
| 1557 | struct inode *snd_inode = d_inode(new_dentry); |
| 1558 | struct timespec time; |
| 1559 | int err; |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1560 | struct fscrypt_name fst_nm, snd_nm; |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1561 | |
| 1562 | ubifs_assert(fst_inode && snd_inode); |
| 1563 | |
Richard Weinberger | ac7e47a | 2016-09-29 20:00:38 +0200 | [diff] [blame] | 1564 | if ((ubifs_crypt_is_encrypted(old_dir) || |
| 1565 | ubifs_crypt_is_encrypted(new_dir)) && |
| 1566 | (old_dir != new_dir) && |
| 1567 | (!fscrypt_has_permitted_context(new_dir, fst_inode) || |
| 1568 | !fscrypt_has_permitted_context(old_dir, snd_inode))) |
| 1569 | return -EPERM; |
| 1570 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1571 | err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm); |
| 1572 | if (err) |
| 1573 | return err; |
| 1574 | |
| 1575 | err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm); |
| 1576 | if (err) { |
| 1577 | fscrypt_free_filename(&fst_nm); |
| 1578 | return err; |
| 1579 | } |
| 1580 | |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1581 | lock_4_inodes(old_dir, new_dir, NULL, NULL); |
| 1582 | |
| 1583 | time = ubifs_current_time(old_dir); |
| 1584 | fst_inode->i_ctime = time; |
| 1585 | snd_inode->i_ctime = time; |
| 1586 | old_dir->i_mtime = old_dir->i_ctime = time; |
| 1587 | new_dir->i_mtime = new_dir->i_ctime = time; |
| 1588 | |
| 1589 | if (old_dir != new_dir) { |
| 1590 | if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) { |
| 1591 | inc_nlink(new_dir); |
| 1592 | drop_nlink(old_dir); |
| 1593 | } |
| 1594 | else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) { |
| 1595 | drop_nlink(new_dir); |
| 1596 | inc_nlink(old_dir); |
| 1597 | } |
| 1598 | } |
| 1599 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1600 | err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir, |
| 1601 | snd_inode, &snd_nm, sync); |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1602 | |
| 1603 | unlock_4_inodes(old_dir, new_dir, NULL, NULL); |
| 1604 | ubifs_release_budget(c, &req); |
| 1605 | |
Richard Weinberger | f4f61d2 | 2016-11-11 22:50:29 +0100 | [diff] [blame] | 1606 | fscrypt_free_filename(&fst_nm); |
| 1607 | fscrypt_free_filename(&snd_nm); |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1608 | return err; |
| 1609 | } |
| 1610 | |
Richard Weinberger | 390975a | 2016-10-18 21:21:43 +0200 | [diff] [blame] | 1611 | static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1612 | struct inode *new_dir, struct dentry *new_dentry, |
| 1613 | unsigned int flags) |
| 1614 | { |
| 1615 | if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE)) |
| 1616 | return -EINVAL; |
| 1617 | |
| 1618 | ubifs_assert(inode_is_locked(old_dir)); |
| 1619 | ubifs_assert(inode_is_locked(new_dir)); |
| 1620 | |
| 1621 | if (flags & RENAME_EXCHANGE) |
| 1622 | return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry); |
| 1623 | |
Richard Weinberger | 390975a | 2016-10-18 21:21:43 +0200 | [diff] [blame] | 1624 | return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
Richard Weinberger | 9ec6496 | 2016-09-14 22:28:51 +0200 | [diff] [blame] | 1625 | } |
| 1626 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 1627 | int ubifs_getattr(const struct path *path, struct kstat *stat, |
| 1628 | u32 request_mask, unsigned int flags) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1629 | { |
| 1630 | loff_t size; |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 1631 | struct inode *inode = d_inode(path->dentry); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1632 | struct ubifs_inode *ui = ubifs_inode(inode); |
| 1633 | |
| 1634 | mutex_lock(&ui->ui_mutex); |
Al Viro | 6d42e7e | 2012-04-02 14:25:07 -0400 | [diff] [blame] | 1635 | generic_fillattr(inode, stat); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1636 | stat->blksize = UBIFS_BLOCK_SIZE; |
| 1637 | stat->size = ui->ui_size; |
| 1638 | |
| 1639 | /* |
| 1640 | * Unfortunately, the 'stat()' system call was designed for block |
| 1641 | * device based file systems, and it is not appropriate for UBIFS, |
| 1642 | * because UBIFS does not have notion of "block". For example, it is |
| 1643 | * difficult to tell how many block a directory takes - it actually |
| 1644 | * takes less than 300 bytes, but we have to round it to block size, |
| 1645 | * which introduces large mistake. This makes utilities like 'du' to |
| 1646 | * report completely senseless numbers. This is the reason why UBIFS |
| 1647 | * goes the same way as JFFS2 - it reports zero blocks for everything |
| 1648 | * but regular files, which makes more sense than reporting completely |
| 1649 | * wrong sizes. |
| 1650 | */ |
| 1651 | if (S_ISREG(inode->i_mode)) { |
| 1652 | size = ui->xattr_size; |
| 1653 | size += stat->size; |
| 1654 | size = ALIGN(size, UBIFS_BLOCK_SIZE); |
| 1655 | /* |
| 1656 | * Note, user-space expects 512-byte blocks count irrespectively |
| 1657 | * of what was reported in @stat->size. |
| 1658 | */ |
| 1659 | stat->blocks = size >> 9; |
| 1660 | } else |
| 1661 | stat->blocks = 0; |
| 1662 | mutex_unlock(&ui->ui_mutex); |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
Richard Weinberger | ba40e6a | 2016-09-29 17:20:27 +0200 | [diff] [blame] | 1666 | static int ubifs_dir_open(struct inode *dir, struct file *file) |
| 1667 | { |
| 1668 | if (ubifs_crypt_is_encrypted(dir)) |
| 1669 | return fscrypt_get_encryption_info(dir) ? -EACCES : 0; |
| 1670 | |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
Artem Bityutskiy | e8b8156 | 2009-01-15 17:43:23 +0200 | [diff] [blame] | 1674 | const struct inode_operations ubifs_dir_inode_operations = { |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1675 | .lookup = ubifs_lookup, |
| 1676 | .create = ubifs_create, |
| 1677 | .link = ubifs_link, |
| 1678 | .symlink = ubifs_symlink, |
| 1679 | .unlink = ubifs_unlink, |
| 1680 | .mkdir = ubifs_mkdir, |
| 1681 | .rmdir = ubifs_rmdir, |
| 1682 | .mknod = ubifs_mknod, |
Richard Weinberger | 390975a | 2016-10-18 21:21:43 +0200 | [diff] [blame] | 1683 | .rename = ubifs_rename, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1684 | .setattr = ubifs_setattr, |
| 1685 | .getattr = ubifs_getattr, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1686 | .listxattr = ubifs_listxattr, |
Dongsheng Yang | 8c1c5f2 | 2015-11-07 12:46:11 +0800 | [diff] [blame] | 1687 | #ifdef CONFIG_UBIFS_ATIME_SUPPORT |
| 1688 | .update_time = ubifs_update_time, |
| 1689 | #endif |
Richard Weinberger | 474b937 | 2016-09-14 22:28:49 +0200 | [diff] [blame] | 1690 | .tmpfile = ubifs_tmpfile, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1691 | }; |
| 1692 | |
Artem Bityutskiy | e8b8156 | 2009-01-15 17:43:23 +0200 | [diff] [blame] | 1693 | const struct file_operations ubifs_dir_operations = { |
Al Viro | 01122e0 | 2013-05-16 01:14:46 -0400 | [diff] [blame] | 1694 | .llseek = generic_file_llseek, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1695 | .release = ubifs_dir_release, |
| 1696 | .read = generic_read_dir, |
Al Viro | c51da20 | 2016-04-30 22:37:34 -0400 | [diff] [blame] | 1697 | .iterate_shared = ubifs_readdir, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1698 | .fsync = ubifs_fsync, |
| 1699 | .unlocked_ioctl = ubifs_ioctl, |
Richard Weinberger | ba40e6a | 2016-09-29 17:20:27 +0200 | [diff] [blame] | 1700 | .open = ubifs_dir_open, |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1701 | #ifdef CONFIG_COMPAT |
| 1702 | .compat_ioctl = ubifs_compat_ioctl, |
| 1703 | #endif |
| 1704 | }; |