blob: d2f5a50f5d83ec6efe415f82e492ca21070f09e8 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/* * 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 Viroad44be52011-07-26 03:12:59 -040059static int inherit_flags(const struct inode *dir, umode_t mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030060{
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 Weinbergerd475a502016-10-20 16:47:56 +020088struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
Al Viroad44be52011-07-26 03:12:59 -040089 umode_t mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030090{
Richard Weinbergerd475a502016-10-20 16:47:56 +020091 int err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030092 struct inode *inode;
93 struct ubifs_inode *ui;
Richard Weinbergerd475a502016-10-20 16:47:56 +020094 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 Bityutskiy1e517642008-07-14 19:08:37 +0300108
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 Bityutskiy12e776a2011-05-27 15:50:39 +0300120 inode->i_flags |= S_NOCMTIME;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300121
Dmitry Monakhovabf5d082010-03-04 17:32:21 +0300122 inode_init_owner(inode, dir, mode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300123 inode->i_mtime = inode->i_atime = inode->i_ctime =
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700124 current_time(inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300125 inode->i_mapping->nrpages = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300126
127 switch (mode & S_IFMT) {
128 case S_IFREG:
129 inode->i_mapping->a_ops = &ubifs_file_address_operations;
130 inode->i_op = &ubifs_file_inode_operations;
131 inode->i_fop = &ubifs_file_operations;
132 break;
133 case S_IFDIR:
134 inode->i_op = &ubifs_dir_inode_operations;
135 inode->i_fop = &ubifs_dir_operations;
136 inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
137 break;
138 case S_IFLNK:
139 inode->i_op = &ubifs_symlink_inode_operations;
140 break;
141 case S_IFSOCK:
142 case S_IFIFO:
143 case S_IFBLK:
144 case S_IFCHR:
145 inode->i_op = &ubifs_file_inode_operations;
David Gstirf34e87f2017-05-17 13:36:16 +0200146 encrypted = false;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300147 break;
148 default:
149 BUG();
150 }
151
152 ui->flags = inherit_flags(dir, mode);
153 ubifs_set_inode_flags(inode);
154 if (S_ISREG(mode))
155 ui->compr_type = c->default_compr;
156 else
157 ui->compr_type = UBIFS_COMPR_NONE;
158 ui->synced_i_size = 0;
159
160 spin_lock(&c->cnt_lock);
161 /* Inode number overflow is currently not supported */
162 if (c->highest_inum >= INUM_WARN_WATERMARK) {
163 if (c->highest_inum >= INUM_WATERMARK) {
164 spin_unlock(&c->cnt_lock);
Sheng Yong235c3622015-03-20 10:39:42 +0000165 ubifs_err(c, "out of inode numbers");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300166 make_bad_inode(inode);
167 iput(inode);
168 return ERR_PTR(-EINVAL);
169 }
Sheng Yong1a7e985d2015-04-03 03:13:42 +0000170 ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200171 (unsigned long)c->highest_inum, INUM_WATERMARK);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300172 }
173
174 inode->i_ino = ++c->highest_inum;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300175 /*
176 * The creation sequence number remains with this inode for its
177 * lifetime. All nodes for this inode have a greater sequence number,
178 * and so it is possible to distinguish obsolete nodes belonging to a
179 * previous incarnation of the same inode number - for example, for the
180 * purpose of rebuilding the index.
181 */
182 ui->creat_sqnum = ++c->max_sqnum;
183 spin_unlock(&c->cnt_lock);
Richard Weinbergerd475a502016-10-20 16:47:56 +0200184
185 if (encrypted) {
186 err = fscrypt_inherit_context(dir, inode, &encrypted, true);
187 if (err) {
188 ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
189 make_bad_inode(inode);
190 iput(inode);
191 return ERR_PTR(err);
192 }
193 }
194
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300195 return inode;
196}
197
Artem Bityutskiybb2615d2011-05-31 17:47:53 +0300198static int dbg_check_name(const struct ubifs_info *c,
199 const struct ubifs_dent_node *dent,
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100200 const struct fscrypt_name *nm)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300201{
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +0300202 if (!dbg_is_chk_gen(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300203 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100204 if (le16_to_cpu(dent->nlen) != fname_len(nm))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300205 return -EINVAL;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100206 if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300207 return -EINVAL;
208 return 0;
209}
210
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300211static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400212 unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300213{
214 int err;
215 union ubifs_key key;
216 struct inode *inode = NULL;
Al Viro191ac102018-04-30 20:14:59 -0400217 struct ubifs_dent_node *dent = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300218 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100219 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300220
Al Viro4cb2a012013-09-16 10:58:53 -0400221 dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300222
Eric Biggersa0b3ccd2017-11-29 12:43:16 -0800223 err = fscrypt_prepare_lookup(dir, dentry, flags);
224 if (err)
225 return ERR_PTR(err);
Richard Weinberger9270b2f2016-09-29 20:10:06 +0200226
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100227 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
228 if (err)
229 return ERR_PTR(err);
230
231 if (fname_len(&nm) > UBIFS_MAX_NLEN) {
Al Viro191ac102018-04-30 20:14:59 -0400232 inode = ERR_PTR(-ENAMETOOLONG);
233 goto done;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100234 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300235
236 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100237 if (!dent) {
Al Viro191ac102018-04-30 20:14:59 -0400238 inode = ERR_PTR(-ENOMEM);
239 goto done;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100240 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300241
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100242 if (nm.hash) {
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200243 ubifs_assert(c, fname_len(&nm) == 0);
244 ubifs_assert(c, fname_name(&nm) == NULL);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100245 dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
Richard Weinberger528e3d12016-11-11 20:46:06 +0100246 err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100247 } else {
248 dent_key_init(c, &key, dir->i_ino, &nm);
249 err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
250 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300251
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300252 if (err) {
Al Viro191ac102018-04-30 20:14:59 -0400253 if (err == -ENOENT)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300254 dbg_gen("not found");
Al Viro191ac102018-04-30 20:14:59 -0400255 else
256 inode = ERR_PTR(err);
257 goto done;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300258 }
259
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100260 if (dbg_check_name(c, dent, &nm)) {
Al Viro191ac102018-04-30 20:14:59 -0400261 inode = ERR_PTR(-EINVAL);
262 goto done;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300263 }
264
265 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
266 if (IS_ERR(inode)) {
267 /*
268 * This should not happen. Probably the file-system needs
269 * checking.
270 */
271 err = PTR_ERR(inode);
Sheng Yong235c3622015-03-20 10:39:42 +0000272 ubifs_err(c, "dead directory entry '%pd', error %d",
Al Viro4cb2a012013-09-16 10:58:53 -0400273 dentry, err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300274 ubifs_ro_mode(c, err);
Al Viro191ac102018-04-30 20:14:59 -0400275 goto done;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300276 }
277
Eric Biggers413d5a92017-04-07 10:58:40 -0700278 if (ubifs_crypt_is_encrypted(dir) &&
279 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
280 !fscrypt_has_permitted_context(dir, inode)) {
281 ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
282 dir->i_ino, inode->i_ino);
Al Viro191ac102018-04-30 20:14:59 -0400283 iput(inode);
284 inode = ERR_PTR(-EPERM);
Eric Biggers413d5a92017-04-07 10:58:40 -0700285 }
286
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300287done:
288 kfree(dent);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100289 fscrypt_free_filename(&nm);
Al Viro191ac102018-04-30 20:14:59 -0400290 return d_splice_alias(inode, dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300291}
292
Al Viro4acdaf22011-07-26 01:42:34 -0400293static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400294 bool excl)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300295{
296 struct inode *inode;
297 struct ubifs_info *c = dir->i_sb->s_fs_info;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300298 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
299 .dirtied_ino = 1 };
300 struct ubifs_inode *dir_ui = ubifs_inode(dir);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100301 struct fscrypt_name nm;
302 int err, sz_change;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300303
304 /*
305 * Budget request settings: new inode, new direntry, changing the
306 * parent directory inode.
307 */
308
Al Viro4cb2a012013-09-16 10:58:53 -0400309 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
310 dentry, mode, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300311
312 err = ubifs_budget_space(c, &req);
313 if (err)
314 return err;
315
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100316 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
317 if (err)
318 goto out_budg;
319
320 sz_change = CALC_DENT_SIZE(fname_len(&nm));
321
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300322 inode = ubifs_new_inode(c, dir, mode);
323 if (IS_ERR(inode)) {
324 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100325 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300326 }
327
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500328 err = ubifs_init_security(dir, inode, &dentry->d_name);
329 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +0100330 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500331
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300332 mutex_lock(&dir_ui->ui_mutex);
333 dir->i_size += sz_change;
334 dir_ui->ui_size = dir->i_size;
335 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100336 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300337 if (err)
338 goto out_cancel;
339 mutex_unlock(&dir_ui->ui_mutex);
340
341 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100342 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300343 insert_inode_hash(inode);
344 d_instantiate(dentry, inode);
345 return 0;
346
347out_cancel:
348 dir->i_size -= sz_change;
349 dir_ui->ui_size = dir->i_size;
350 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +0100351out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300352 make_bad_inode(inode);
353 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100354out_fname:
355 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300356out_budg:
357 ubifs_release_budget(c, &req);
Sheng Yong235c3622015-03-20 10:39:42 +0000358 ubifs_err(c, "cannot create regular file, error %d", err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300359 return err;
360}
361
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200362static int do_tmpfile(struct inode *dir, struct dentry *dentry,
363 umode_t mode, struct inode **whiteout)
Richard Weinberger474b9372016-09-14 22:28:49 +0200364{
365 struct inode *inode;
366 struct ubifs_info *c = dir->i_sb->s_fs_info;
367 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
368 struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
369 struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
370 int err, instantiated = 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100371 struct fscrypt_name nm;
Richard Weinberger474b9372016-09-14 22:28:49 +0200372
373 /*
374 * Budget request settings: new dirty inode, new direntry,
375 * budget for dirtied inode will be released via writeback.
376 */
377
378 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
379 dentry, mode, dir->i_ino);
380
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100381 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200382 if (err)
383 return err;
384
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100385 err = ubifs_budget_space(c, &req);
386 if (err) {
387 fscrypt_free_filename(&nm);
388 return err;
389 }
390
Richard Weinberger474b9372016-09-14 22:28:49 +0200391 err = ubifs_budget_space(c, &ino_req);
392 if (err) {
393 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100394 fscrypt_free_filename(&nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200395 return err;
396 }
397
398 inode = ubifs_new_inode(c, dir, mode);
399 if (IS_ERR(inode)) {
400 err = PTR_ERR(inode);
401 goto out_budg;
402 }
403 ui = ubifs_inode(inode);
404
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200405 if (whiteout) {
406 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200407 ubifs_assert(c, inode->i_op == &ubifs_file_inode_operations);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200408 }
409
Richard Weinberger474b9372016-09-14 22:28:49 +0200410 err = ubifs_init_security(dir, inode, &dentry->d_name);
411 if (err)
412 goto out_inode;
413
414 mutex_lock(&ui->ui_mutex);
415 insert_inode_hash(inode);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200416
417 if (whiteout) {
418 mark_inode_dirty(inode);
419 drop_nlink(inode);
420 *whiteout = inode;
421 } else {
422 d_tmpfile(dentry, inode);
423 }
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200424 ubifs_assert(c, ui->dirty);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200425
Richard Weinberger474b9372016-09-14 22:28:49 +0200426 instantiated = 1;
427 mutex_unlock(&ui->ui_mutex);
428
429 mutex_lock(&dir_ui->ui_mutex);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100430 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Richard Weinberger474b9372016-09-14 22:28:49 +0200431 if (err)
432 goto out_cancel;
433 mutex_unlock(&dir_ui->ui_mutex);
434
435 ubifs_release_budget(c, &req);
436
437 return 0;
438
439out_cancel:
440 mutex_unlock(&dir_ui->ui_mutex);
441out_inode:
442 make_bad_inode(inode);
443 if (!instantiated)
444 iput(inode);
445out_budg:
446 ubifs_release_budget(c, &req);
447 if (!instantiated)
448 ubifs_release_budget(c, &ino_req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100449 fscrypt_free_filename(&nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200450 ubifs_err(c, "cannot create temporary file, error %d", err);
451 return err;
452}
453
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200454static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
455 umode_t mode)
456{
457 return do_tmpfile(dir, dentry, mode, NULL);
458}
459
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300460/**
461 * vfs_dent_type - get VFS directory entry type.
462 * @type: UBIFS directory entry type
463 *
464 * This function converts UBIFS directory entry type into VFS directory entry
465 * type.
466 */
467static unsigned int vfs_dent_type(uint8_t type)
468{
469 switch (type) {
470 case UBIFS_ITYPE_REG:
471 return DT_REG;
472 case UBIFS_ITYPE_DIR:
473 return DT_DIR;
474 case UBIFS_ITYPE_LNK:
475 return DT_LNK;
476 case UBIFS_ITYPE_BLK:
477 return DT_BLK;
478 case UBIFS_ITYPE_CHR:
479 return DT_CHR;
480 case UBIFS_ITYPE_FIFO:
481 return DT_FIFO;
482 case UBIFS_ITYPE_SOCK:
483 return DT_SOCK;
484 default:
485 BUG();
486 }
487 return 0;
488}
489
490/*
491 * The classical Unix view for directory is that it is a linear array of
492 * (name, inode number) entries. Linux/VFS assumes this model as well.
493 * Particularly, 'readdir()' call wants us to return a directory entry offset
494 * which later may be used to continue 'readdir()'ing the directory or to
495 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
496 * model because directory entries are identified by keys, which may collide.
497 *
498 * UBIFS uses directory entry hash value for directory offsets, so
499 * 'seekdir()'/'telldir()' may not always work because of possible key
500 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
501 * properly by means of saving full directory entry name in the private field
502 * of the file description object.
503 *
504 * This means that UBIFS cannot support NFS which requires full
505 * 'seekdir()'/'telldir()' support.
506 */
Al Viro01122e02013-05-16 01:14:46 -0400507static int ubifs_readdir(struct file *file, struct dir_context *ctx)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300508{
Richard Weinbergerba75d572016-12-14 11:09:25 +0100509 int fstr_real_len = 0, err = 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100510 struct fscrypt_name nm;
511 struct fscrypt_str fstr = {0};
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300512 union ubifs_key key;
513 struct ubifs_dent_node *dent;
Al Viro496ad9a2013-01-23 17:07:38 -0500514 struct inode *dir = file_inode(file);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300515 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100516 bool encrypted = ubifs_crypt_is_encrypted(dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300517
Al Viro01122e02013-05-16 01:14:46 -0400518 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300519
Al Viro01122e02013-05-16 01:14:46 -0400520 if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300521 /*
522 * The directory was seek'ed to a senseless position or there
523 * are no more entries.
524 */
525 return 0;
526
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100527 if (encrypted) {
528 err = fscrypt_get_encryption_info(dir);
529 if (err && err != -ENOKEY)
530 return err;
531
532 err = fscrypt_fname_alloc_buffer(dir, UBIFS_MAX_NLEN, &fstr);
533 if (err)
534 return err;
535
536 fstr_real_len = fstr.len;
537 }
538
Artem Bityutskiy605c9122013-06-28 14:15:15 +0300539 if (file->f_version == 0) {
540 /*
541 * The file was seek'ed, which means that @file->private_data
542 * is now invalid. This may also be just the first
543 * 'ubifs_readdir()' invocation, in which case
544 * @file->private_data is NULL, and the below code is
545 * basically a no-op.
546 */
547 kfree(file->private_data);
548 file->private_data = NULL;
549 }
550
551 /*
552 * 'generic_file_llseek()' unconditionally sets @file->f_version to
553 * zero, and we use this for detecting whether the file was seek'ed.
554 */
555 file->f_version = 1;
556
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300557 /* File positions 0 and 1 correspond to "." and ".." */
Al Viro01122e02013-05-16 01:14:46 -0400558 if (ctx->pos < 2) {
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200559 ubifs_assert(c, !file->private_data);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100560 if (!dir_emit_dots(file, ctx)) {
561 if (encrypted)
562 fscrypt_fname_free_buffer(&fstr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300563 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100564 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300565
566 /* Find the first entry in TNC and save it */
567 lowest_dent_key(c, &key, dir->i_ino);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100568 fname_len(&nm) = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300569 dent = ubifs_tnc_next_ent(c, &key, &nm);
570 if (IS_ERR(dent)) {
571 err = PTR_ERR(dent);
572 goto out;
573 }
574
Al Viro01122e02013-05-16 01:14:46 -0400575 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300576 file->private_data = dent;
577 }
578
579 dent = file->private_data;
580 if (!dent) {
581 /*
582 * The directory was seek'ed to and is now readdir'ed.
Al Viro01122e02013-05-16 01:14:46 -0400583 * Find the entry corresponding to @ctx->pos or the closest one.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300584 */
Al Viro01122e02013-05-16 01:14:46 -0400585 dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100586 fname_len(&nm) = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300587 dent = ubifs_tnc_next_ent(c, &key, &nm);
588 if (IS_ERR(dent)) {
589 err = PTR_ERR(dent);
590 goto out;
591 }
Al Viro01122e02013-05-16 01:14:46 -0400592 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300593 file->private_data = dent;
594 }
595
596 while (1) {
Hyunchul Leeb20e2d92017-03-15 10:31:03 +0900597 dbg_gen("ino %llu, new f_pos %#x",
598 (unsigned long long)le64_to_cpu(dent->inum),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300599 key_hash_flash(c, &dent->key));
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200600 ubifs_assert(c, le64_to_cpu(dent->ch.sqnum) >
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700601 ubifs_inode(dir)->creat_sqnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300602
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100603 fname_len(&nm) = le16_to_cpu(dent->nlen);
604 fname_name(&nm) = dent->name;
605
606 if (encrypted) {
607 fstr.len = fstr_real_len;
608
Richard Weinberger528e3d12016-11-11 20:46:06 +0100609 err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
610 &dent->key),
611 le32_to_cpu(dent->cookie),
612 &nm.disk_name, &fstr);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +0200613 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100614 goto out;
615 } else {
616 fstr.len = fname_len(&nm);
617 fstr.name = fname_name(&nm);
618 }
619
620 if (!dir_emit(ctx, fstr.name, fstr.len,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300621 le64_to_cpu(dent->inum),
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100622 vfs_dent_type(dent->type))) {
623 if (encrypted)
624 fscrypt_fname_free_buffer(&fstr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300625 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100626 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300627
628 /* Switch to the next entry */
629 key_read(c, &dent->key, &key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300630 dent = ubifs_tnc_next_ent(c, &key, &nm);
631 if (IS_ERR(dent)) {
632 err = PTR_ERR(dent);
633 goto out;
634 }
635
636 kfree(file->private_data);
Al Viro01122e02013-05-16 01:14:46 -0400637 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300638 file->private_data = dent;
639 cond_resched();
640 }
641
642out:
Richard Weinbergeraeeb14f2015-10-12 23:35:36 +0200643 kfree(file->private_data);
644 file->private_data = NULL;
645
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100646 if (encrypted)
647 fscrypt_fname_free_buffer(&fstr);
648
Richard Weinbergerc83ed4c2016-10-19 12:43:07 +0200649 if (err != -ENOENT)
Sheng Yong235c3622015-03-20 10:39:42 +0000650 ubifs_err(c, "cannot find next direntry, error %d", err);
Richard Weinbergera00052a2016-10-28 11:49:03 +0200651 else
652 /*
653 * -ENOENT is a non-fatal error in this context, the TNC uses
654 * it to indicate that the cursor moved past the current directory
655 * and readdir() has to stop.
656 */
657 err = 0;
658
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300659
Artem Bityutskiy605c9122013-06-28 14:15:15 +0300660 /* 2 is a special value indicating that there are no more direntries */
Al Viro01122e02013-05-16 01:14:46 -0400661 ctx->pos = 2;
Richard Weinbergerc83ed4c2016-10-19 12:43:07 +0200662 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300663}
664
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300665/* Free saved readdir() state when the directory is closed */
666static int ubifs_dir_release(struct inode *dir, struct file *file)
667{
668 kfree(file->private_data);
669 file->private_data = NULL;
670 return 0;
671}
672
673/**
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200674 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300675 * @inode1: first inode
676 * @inode2: second inode
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200677 *
678 * We do not implement any tricks to guarantee strict lock ordering, because
679 * VFS has already done it for us on the @i_mutex. So this is just a simple
680 * wrapper function.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300681 */
682static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
683{
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200684 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
685 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300686}
687
688/**
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200689 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300690 * @inode1: first inode
691 * @inode2: second inode
692 */
693static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
694{
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300695 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200696 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300697}
698
699static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
700 struct dentry *dentry)
701{
702 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000703 struct inode *inode = d_inode(old_dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300704 struct ubifs_inode *ui = ubifs_inode(inode);
705 struct ubifs_inode *dir_ui = ubifs_inode(dir);
706 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
707 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +0300708 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100709 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300710
711 /*
712 * Budget request settings: new direntry, changing the target inode,
713 * changing the parent inode.
714 */
715
Al Viro4cb2a012013-09-16 10:58:53 -0400716 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
717 dentry, inode->i_ino,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300718 inode->i_nlink, dir->i_ino);
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200719 ubifs_assert(c, inode_is_locked(dir));
720 ubifs_assert(c, inode_is_locked(inode));
Hunter Adrian8b3884a2009-05-14 06:32:30 +0200721
Eric Biggers56538782017-11-29 12:43:14 -0800722 err = fscrypt_prepare_link(old_dentry, dir, dentry);
723 if (err)
724 return err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100725
726 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
727 if (err)
728 return err;
Richard Weinbergerac7e47a2016-09-29 20:00:38 +0200729
Artem Bityutskiyd808efb2011-05-31 18:14:38 +0300730 err = dbg_check_synced_i_size(c, inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300731 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100732 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300733
734 err = ubifs_budget_space(c, &req);
735 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100736 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300737
738 lock_2_inodes(dir, inode);
Richard Weinberger32fe9052017-03-30 10:50:49 +0200739
740 /* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
741 if (inode->i_nlink == 0)
742 ubifs_delete_orphan(c, inode->i_ino);
743
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300744 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400745 ihold(inode);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700746 inode->i_ctime = current_time(inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300747 dir->i_size += sz_change;
748 dir_ui->ui_size = dir->i_size;
749 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100750 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300751 if (err)
752 goto out_cancel;
753 unlock_2_inodes(dir, inode);
754
755 ubifs_release_budget(c, &req);
756 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100757 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300758 return 0;
759
760out_cancel:
761 dir->i_size -= sz_change;
762 dir_ui->ui_size = dir->i_size;
763 drop_nlink(inode);
Richard Weinberger32fe9052017-03-30 10:50:49 +0200764 if (inode->i_nlink == 0)
765 ubifs_add_orphan(c, inode->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300766 unlock_2_inodes(dir, inode);
767 ubifs_release_budget(c, &req);
768 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100769out_fname:
770 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300771 return err;
772}
773
774static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
775{
776 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000777 struct inode *inode = d_inode(dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300778 struct ubifs_inode *dir_ui = ubifs_inode(dir);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100779 int err, sz_change, budgeted = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300780 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200781 unsigned int saved_nlink = inode->i_nlink;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100782 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300783
784 /*
785 * Budget request settings: deletion direntry, deletion inode (+1 for
786 * @dirtied_ino), changing the parent directory inode. If budgeting
787 * fails, go ahead anyway because we have extra space reserved for
788 * deletions.
789 */
790
Al Viro4cb2a012013-09-16 10:58:53 -0400791 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
792 dentry, inode->i_ino,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300793 inode->i_nlink, dir->i_ino);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100794
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100795 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
796 if (err)
797 return err;
798
Richard Weinberger9ca2d732019-04-05 00:34:38 +0200799 err = ubifs_purge_xattrs(inode);
800 if (err)
801 return err;
802
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100803 sz_change = CALC_DENT_SIZE(fname_len(&nm));
804
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200805 ubifs_assert(c, inode_is_locked(dir));
806 ubifs_assert(c, inode_is_locked(inode));
Artem Bityutskiyd808efb2011-05-31 18:14:38 +0300807 err = dbg_check_synced_i_size(c, inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300808 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100809 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300810
811 err = ubifs_budget_space(c, &req);
812 if (err) {
813 if (err != -ENOSPC)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100814 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300815 budgeted = 0;
816 }
817
818 lock_2_inodes(dir, inode);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700819 inode->i_ctime = current_time(dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300820 drop_nlink(inode);
821 dir->i_size -= sz_change;
822 dir_ui->ui_size = dir->i_size;
823 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100824 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300825 if (err)
826 goto out_cancel;
827 unlock_2_inodes(dir, inode);
828
829 if (budgeted)
830 ubifs_release_budget(c, &req);
831 else {
832 /* We've deleted something - clean the "no space" flags */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300833 c->bi.nospace = c->bi.nospace_rp = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300834 smp_wmb();
835 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100836 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300837 return 0;
838
839out_cancel:
840 dir->i_size += sz_change;
841 dir_ui->ui_size = dir->i_size;
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200842 set_nlink(inode, saved_nlink);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300843 unlock_2_inodes(dir, inode);
844 if (budgeted)
845 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100846out_fname:
847 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300848 return err;
849}
850
851/**
852 * check_dir_empty - check if a directory is empty or not.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300853 * @dir: VFS inode object of the directory to check
854 *
855 * This function checks if directory @dir is empty. Returns zero if the
856 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
857 * in case of of errors.
858 */
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200859int ubifs_check_dir_empty(struct inode *dir)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300860{
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200861 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100862 struct fscrypt_name nm = { 0 };
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300863 struct ubifs_dent_node *dent;
864 union ubifs_key key;
865 int err;
866
867 lowest_dent_key(c, &key, dir->i_ino);
868 dent = ubifs_tnc_next_ent(c, &key, &nm);
869 if (IS_ERR(dent)) {
870 err = PTR_ERR(dent);
871 if (err == -ENOENT)
872 err = 0;
873 } else {
874 kfree(dent);
875 err = -ENOTEMPTY;
876 }
877 return err;
878}
879
880static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
881{
882 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000883 struct inode *inode = d_inode(dentry);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100884 int err, sz_change, budgeted = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300885 struct ubifs_inode *dir_ui = ubifs_inode(dir);
886 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100887 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300888
889 /*
890 * Budget request settings: deletion direntry, deletion inode and
891 * changing the parent inode. If budgeting fails, go ahead anyway
892 * because we have extra space reserved for deletions.
893 */
894
Al Viro4cb2a012013-09-16 10:58:53 -0400895 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
896 inode->i_ino, dir->i_ino);
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200897 ubifs_assert(c, inode_is_locked(dir));
898 ubifs_assert(c, inode_is_locked(inode));
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200899 err = ubifs_check_dir_empty(d_inode(dentry));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300900 if (err)
901 return err;
902
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100903 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
904 if (err)
905 return err;
906
Richard Weinberger9ca2d732019-04-05 00:34:38 +0200907 err = ubifs_purge_xattrs(inode);
908 if (err)
909 return err;
910
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100911 sz_change = CALC_DENT_SIZE(fname_len(&nm));
912
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300913 err = ubifs_budget_space(c, &req);
914 if (err) {
915 if (err != -ENOSPC)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100916 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300917 budgeted = 0;
918 }
919
920 lock_2_inodes(dir, inode);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700921 inode->i_ctime = current_time(dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300922 clear_nlink(inode);
923 drop_nlink(dir);
924 dir->i_size -= sz_change;
925 dir_ui->ui_size = dir->i_size;
926 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100927 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300928 if (err)
929 goto out_cancel;
930 unlock_2_inodes(dir, inode);
931
932 if (budgeted)
933 ubifs_release_budget(c, &req);
934 else {
935 /* We've deleted something - clean the "no space" flags */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300936 c->bi.nospace = c->bi.nospace_rp = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300937 smp_wmb();
938 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100939 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300940 return 0;
941
942out_cancel:
943 dir->i_size += sz_change;
944 dir_ui->ui_size = dir->i_size;
945 inc_nlink(dir);
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200946 set_nlink(inode, 2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300947 unlock_2_inodes(dir, inode);
948 if (budgeted)
949 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100950out_fname:
951 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300952 return err;
953}
954
Al Viro18bb1db2011-07-26 01:41:39 -0400955static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300956{
957 struct inode *inode;
958 struct ubifs_inode *dir_ui = ubifs_inode(dir);
959 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100960 int err, sz_change;
Artem Bityutskiy182854b2008-07-18 18:54:29 +0300961 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100962 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300963
964 /*
965 * Budget request settings: new inode, new direntry and changing parent
966 * directory inode.
967 */
968
Al Viro4cb2a012013-09-16 10:58:53 -0400969 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
970 dentry, mode, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300971
972 err = ubifs_budget_space(c, &req);
973 if (err)
974 return err;
975
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100976 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
977 if (err)
978 goto out_budg;
979
980 sz_change = CALC_DENT_SIZE(fname_len(&nm));
981
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300982 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
983 if (IS_ERR(inode)) {
984 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100985 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300986 }
987
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500988 err = ubifs_init_security(dir, inode, &dentry->d_name);
989 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +0100990 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500991
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300992 mutex_lock(&dir_ui->ui_mutex);
993 insert_inode_hash(inode);
994 inc_nlink(inode);
995 inc_nlink(dir);
996 dir->i_size += sz_change;
997 dir_ui->ui_size = dir->i_size;
998 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100999 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001000 if (err) {
Sheng Yong235c3622015-03-20 10:39:42 +00001001 ubifs_err(c, "cannot create directory, error %d", err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001002 goto out_cancel;
1003 }
1004 mutex_unlock(&dir_ui->ui_mutex);
1005
1006 ubifs_release_budget(c, &req);
1007 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001008 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001009 return 0;
1010
1011out_cancel:
1012 dir->i_size -= sz_change;
1013 dir_ui->ui_size = dir->i_size;
1014 drop_nlink(dir);
1015 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +01001016out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001017 make_bad_inode(inode);
1018 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001019out_fname:
1020 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001021out_budg:
1022 ubifs_release_budget(c, &req);
1023 return err;
1024}
1025
1026static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001027 umode_t mode, dev_t rdev)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001028{
1029 struct inode *inode;
1030 struct ubifs_inode *ui;
1031 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1032 struct ubifs_info *c = dir->i_sb->s_fs_info;
1033 union ubifs_dev_desc *dev = NULL;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001034 int sz_change;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001035 int err, devlen = 0;
1036 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001037 .dirtied_ino = 1 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001038 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001039
1040 /*
1041 * Budget request settings: new inode, new direntry and changing parent
1042 * directory inode.
1043 */
1044
Al Viro4cb2a012013-09-16 10:58:53 -04001045 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001046
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001047 if (S_ISBLK(mode) || S_ISCHR(mode)) {
1048 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1049 if (!dev)
1050 return -ENOMEM;
1051 devlen = ubifs_encode_dev(dev, rdev);
1052 }
1053
Hyunchul Lee4d35ca42017-05-17 08:57:18 +09001054 req.new_ino_d = ALIGN(devlen, 8);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001055 err = ubifs_budget_space(c, &req);
1056 if (err) {
1057 kfree(dev);
1058 return err;
1059 }
1060
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001061 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
Richard Weinberger63ed6572017-02-10 17:46:01 +01001062 if (err) {
1063 kfree(dev);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001064 goto out_budg;
Richard Weinberger63ed6572017-02-10 17:46:01 +01001065 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001066
1067 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1068
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001069 inode = ubifs_new_inode(c, dir, mode);
1070 if (IS_ERR(inode)) {
1071 kfree(dev);
1072 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001073 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001074 }
1075
1076 init_special_inode(inode, inode->i_mode, rdev);
1077 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
1078 ui = ubifs_inode(inode);
1079 ui->data = dev;
1080 ui->data_len = devlen;
1081
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001082 err = ubifs_init_security(dir, inode, &dentry->d_name);
1083 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001084 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001085
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001086 mutex_lock(&dir_ui->ui_mutex);
1087 dir->i_size += sz_change;
1088 dir_ui->ui_size = dir->i_size;
1089 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001090 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001091 if (err)
1092 goto out_cancel;
1093 mutex_unlock(&dir_ui->ui_mutex);
1094
1095 ubifs_release_budget(c, &req);
1096 insert_inode_hash(inode);
1097 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001098 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001099 return 0;
1100
1101out_cancel:
1102 dir->i_size -= sz_change;
1103 dir_ui->ui_size = dir->i_size;
1104 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +01001105out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001106 make_bad_inode(inode);
1107 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001108out_fname:
1109 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001110out_budg:
1111 ubifs_release_budget(c, &req);
1112 return err;
1113}
1114
1115static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
1116 const char *symname)
1117{
1118 struct inode *inode;
1119 struct ubifs_inode *ui;
1120 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1121 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinberger00ee8b62018-06-11 23:41:09 +02001122 int err, sz_change, len = strlen(symname);
Eric Biggers0e4dda22018-01-11 23:27:00 -05001123 struct fscrypt_str disk_link;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001124 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001125 .new_ino_d = ALIGN(len, 8),
1126 .dirtied_ino = 1 };
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001127 struct fscrypt_name nm;
1128
Eric Biggers0e4dda22018-01-11 23:27:00 -05001129 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
1130 symname, dir->i_ino);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001131
Eric Biggers0e4dda22018-01-11 23:27:00 -05001132 err = fscrypt_prepare_symlink(dir, symname, len, UBIFS_MAX_INO_DATA,
1133 &disk_link);
1134 if (err)
1135 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001136
1137 /*
1138 * Budget request settings: new inode, new direntry and changing parent
1139 * directory inode.
1140 */
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001141 err = ubifs_budget_space(c, &req);
1142 if (err)
1143 return err;
1144
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001145 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1146 if (err)
1147 goto out_budg;
1148
Richard Weinberger00ee8b62018-06-11 23:41:09 +02001149 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1150
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001151 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
1152 if (IS_ERR(inode)) {
1153 err = PTR_ERR(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001154 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001155 }
1156
1157 ui = ubifs_inode(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001158 ui->data = kmalloc(disk_link.len, GFP_NOFS);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001159 if (!ui->data) {
1160 err = -ENOMEM;
1161 goto out_inode;
1162 }
1163
Eric Biggers0e4dda22018-01-11 23:27:00 -05001164 if (IS_ENCRYPTED(inode)) {
1165 disk_link.name = ui->data; /* encrypt directly into ui->data */
1166 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
Eric Biggers6b46d442018-01-11 23:27:00 -05001167 if (err)
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001168 goto out_inode;
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001169 } else {
Eric Biggers0e4dda22018-01-11 23:27:00 -05001170 memcpy(ui->data, disk_link.name, disk_link.len);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001171 inode->i_link = ui->data;
1172 }
1173
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001174 /*
1175 * The terminating zero byte is not written to the flash media and it
1176 * is put just to make later in-memory string processing simpler. Thus,
Eric Biggers0e4dda22018-01-11 23:27:00 -05001177 * data length is @disk_link.len - 1, not @disk_link.len.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001178 */
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001179 ui->data_len = disk_link.len - 1;
1180 inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001181
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001182 err = ubifs_init_security(dir, inode, &dentry->d_name);
1183 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001184 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001185
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001186 mutex_lock(&dir_ui->ui_mutex);
1187 dir->i_size += sz_change;
1188 dir_ui->ui_size = dir->i_size;
1189 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001190 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001191 if (err)
1192 goto out_cancel;
1193 mutex_unlock(&dir_ui->ui_mutex);
1194
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001195 insert_inode_hash(inode);
1196 d_instantiate(dentry, inode);
Eric Biggers6b46d442018-01-11 23:27:00 -05001197 err = 0;
1198 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001199
1200out_cancel:
1201 dir->i_size -= sz_change;
1202 dir_ui->ui_size = dir->i_size;
1203 mutex_unlock(&dir_ui->ui_mutex);
1204out_inode:
1205 make_bad_inode(inode);
1206 iput(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001207out_fname:
1208 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001209out_budg:
1210 ubifs_release_budget(c, &req);
1211 return err;
1212}
1213
1214/**
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001215 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001216 * @inode1: first inode
1217 * @inode2: second inode
1218 * @inode3: third inode
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001219 * @inode4: fouth inode
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001220 *
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001221 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001222 * @inode2 whereas @inode3 and @inode4 may be %NULL.
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001223 *
1224 * We do not implement any tricks to guarantee strict lock ordering, because
1225 * VFS has already done it for us on the @i_mutex. So this is just a simple
1226 * wrapper function.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001227 */
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001228static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1229 struct inode *inode3, struct inode *inode4)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001230{
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001231 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1232 if (inode2 != inode1)
1233 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1234 if (inode3)
1235 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001236 if (inode4)
1237 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001238}
1239
1240/**
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001241 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001242 * @inode1: first inode
1243 * @inode2: second inode
1244 * @inode3: third inode
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001245 * @inode4: fouth inode
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001246 */
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001247static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1248 struct inode *inode3, struct inode *inode4)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001249{
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001250 if (inode4)
1251 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001252 if (inode3)
1253 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001254 if (inode1 != inode2)
1255 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1256 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001257}
1258
Richard Weinberger390975a2016-10-18 21:21:43 +02001259static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1260 struct inode *new_dir, struct dentry *new_dentry,
1261 unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001262{
1263 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +00001264 struct inode *old_inode = d_inode(old_dentry);
1265 struct inode *new_inode = d_inode(new_dentry);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001266 struct inode *whiteout = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001267 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001268 struct ubifs_inode *whiteout_ui = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001269 int err, release, sync = 0, move = (new_dir != old_dir);
1270 int is_dir = S_ISDIR(old_inode->i_mode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001271 int unlink = !!new_inode, new_sz, old_sz;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001272 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1273 .dirtied_ino = 3 };
1274 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001275 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
Deepa Dinamani95582b02018-05-08 19:36:02 -07001276 struct timespec64 time;
Alexandre Pereira da Silva782759b2012-06-25 17:47:49 -03001277 unsigned int uninitialized_var(saved_nlink);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001278 struct fscrypt_name old_nm, new_nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001279
1280 /*
1281 * Budget request settings: deletion direntry, new direntry, removing
1282 * the old inode, and changing old and new parent directory inodes.
1283 *
1284 * However, this operation also marks the target inode as dirty and
1285 * does not write it, so we allocate budget for the target inode
1286 * separately.
1287 */
1288
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001289 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
Al Viro4cb2a012013-09-16 10:58:53 -04001290 old_dentry, old_inode->i_ino, old_dir->i_ino,
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001291 new_dentry, new_dir->i_ino, flags);
1292
Richard Weinberger9ca2d732019-04-05 00:34:38 +02001293 if (unlink) {
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001294 ubifs_assert(c, inode_is_locked(new_inode));
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001295
Richard Weinberger9ca2d732019-04-05 00:34:38 +02001296 err = ubifs_purge_xattrs(new_inode);
1297 if (err)
1298 return err;
1299 }
1300
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001301 if (unlink && is_dir) {
Richard Weinbergerf6337d82016-09-19 20:54:19 +02001302 err = ubifs_check_dir_empty(new_inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001303 if (err)
1304 return err;
1305 }
1306
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001307 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001308 if (err)
1309 return err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001310
1311 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
1312 if (err) {
1313 fscrypt_free_filename(&old_nm);
1314 return err;
1315 }
1316
1317 new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
1318 old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
1319
1320 err = ubifs_budget_space(c, &req);
1321 if (err) {
1322 fscrypt_free_filename(&old_nm);
1323 fscrypt_free_filename(&new_nm);
1324 return err;
1325 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001326 err = ubifs_budget_space(c, &ino_req);
1327 if (err) {
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001328 fscrypt_free_filename(&old_nm);
1329 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001330 ubifs_release_budget(c, &req);
1331 return err;
1332 }
1333
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001334 if (flags & RENAME_WHITEOUT) {
1335 union ubifs_dev_desc *dev = NULL;
1336
1337 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1338 if (!dev) {
Hyunchul Leebb50c632017-05-17 08:58:02 +09001339 err = -ENOMEM;
1340 goto out_release;
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001341 }
1342
1343 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1344 if (err) {
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001345 kfree(dev);
Hyunchul Leebb50c632017-05-17 08:58:02 +09001346 goto out_release;
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001347 }
1348
1349 whiteout->i_state |= I_LINKABLE;
1350 whiteout_ui = ubifs_inode(whiteout);
1351 whiteout_ui->data = dev;
1352 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001353 ubifs_assert(c, !whiteout_ui->dirty);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001354 }
1355
1356 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001357
1358 /*
1359 * Like most other Unix systems, set the @i_ctime for inodes on a
1360 * rename.
1361 */
Deepa Dinamani607a11a2017-05-08 15:59:25 -07001362 time = current_time(old_dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001363 old_inode->i_ctime = time;
1364
1365 /* We must adjust parent link count when renaming directories */
1366 if (is_dir) {
1367 if (move) {
1368 /*
1369 * @old_dir loses a link because we are moving
1370 * @old_inode to a different directory.
1371 */
1372 drop_nlink(old_dir);
1373 /*
1374 * @new_dir only gains a link if we are not also
1375 * overwriting an existing directory.
1376 */
1377 if (!unlink)
1378 inc_nlink(new_dir);
1379 } else {
1380 /*
1381 * @old_inode is not moving to a different directory,
1382 * but @old_dir still loses a link if we are
1383 * overwriting an existing directory.
1384 */
1385 if (unlink)
1386 drop_nlink(old_dir);
1387 }
1388 }
1389
1390 old_dir->i_size -= old_sz;
1391 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1392 old_dir->i_mtime = old_dir->i_ctime = time;
1393 new_dir->i_mtime = new_dir->i_ctime = time;
1394
1395 /*
1396 * And finally, if we unlinked a direntry which happened to have the
1397 * same name as the moved direntry, we have to decrement @i_nlink of
1398 * the unlinked inode and change its ctime.
1399 */
1400 if (unlink) {
1401 /*
1402 * Directories cannot have hard-links, so if this is a
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001403 * directory, just clear @i_nlink.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001404 */
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001405 saved_nlink = new_inode->i_nlink;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001406 if (is_dir)
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001407 clear_nlink(new_inode);
1408 else
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001409 drop_nlink(new_inode);
1410 new_inode->i_ctime = time;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001411 } else {
1412 new_dir->i_size += new_sz;
1413 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1414 }
1415
1416 /*
1417 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1418 * is dirty, because this will be done later on at the end of
1419 * 'ubifs_rename()'.
1420 */
1421 if (IS_SYNC(old_inode)) {
1422 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1423 if (unlink && IS_SYNC(new_inode))
1424 sync = 1;
1425 }
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001426
1427 if (whiteout) {
1428 struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1429 .dirtied_ino_d = \
1430 ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1431
1432 err = ubifs_budget_space(c, &wht_req);
1433 if (err) {
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001434 kfree(whiteout_ui->data);
1435 whiteout_ui->data_len = 0;
1436 iput(whiteout);
Hyunchul Leebb50c632017-05-17 08:58:02 +09001437 goto out_release;
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001438 }
1439
1440 inc_nlink(whiteout);
1441 mark_inode_dirty(whiteout);
1442 whiteout->i_state &= ~I_LINKABLE;
1443 iput(whiteout);
1444 }
1445
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001446 err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
1447 new_inode, &new_nm, whiteout, sync);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001448 if (err)
1449 goto out_cancel;
1450
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001451 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001452 ubifs_release_budget(c, &req);
1453
1454 mutex_lock(&old_inode_ui->ui_mutex);
1455 release = old_inode_ui->dirty;
1456 mark_inode_dirty_sync(old_inode);
1457 mutex_unlock(&old_inode_ui->ui_mutex);
1458
1459 if (release)
1460 ubifs_release_budget(c, &ino_req);
1461 if (IS_SYNC(old_inode))
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001462 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001463
1464 fscrypt_free_filename(&old_nm);
1465 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001466 return err;
1467
1468out_cancel:
1469 if (unlink) {
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001470 set_nlink(new_inode, saved_nlink);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001471 } else {
1472 new_dir->i_size -= new_sz;
1473 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1474 }
1475 old_dir->i_size += old_sz;
1476 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1477 if (is_dir) {
1478 if (move) {
1479 inc_nlink(old_dir);
1480 if (!unlink)
1481 drop_nlink(new_dir);
1482 } else {
1483 if (unlink)
1484 inc_nlink(old_dir);
1485 }
1486 }
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001487 if (whiteout) {
1488 drop_nlink(whiteout);
1489 iput(whiteout);
1490 }
1491 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Hyunchul Leebb50c632017-05-17 08:58:02 +09001492out_release:
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001493 ubifs_release_budget(c, &ino_req);
1494 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001495 fscrypt_free_filename(&old_nm);
1496 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001497 return err;
1498}
1499
Richard Weinberger9ec64962016-09-14 22:28:51 +02001500static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1501 struct inode *new_dir, struct dentry *new_dentry)
1502{
1503 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1504 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1505 .dirtied_ino = 2 };
1506 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1507 struct inode *fst_inode = d_inode(old_dentry);
1508 struct inode *snd_inode = d_inode(new_dentry);
Deepa Dinamani95582b02018-05-08 19:36:02 -07001509 struct timespec64 time;
Richard Weinberger9ec64962016-09-14 22:28:51 +02001510 int err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001511 struct fscrypt_name fst_nm, snd_nm;
Richard Weinberger9ec64962016-09-14 22:28:51 +02001512
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001513 ubifs_assert(c, fst_inode && snd_inode);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001514
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001515 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
1516 if (err)
1517 return err;
1518
1519 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
1520 if (err) {
1521 fscrypt_free_filename(&fst_nm);
1522 return err;
1523 }
1524
Richard Weinberger9ec64962016-09-14 22:28:51 +02001525 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1526
Deepa Dinamani607a11a2017-05-08 15:59:25 -07001527 time = current_time(old_dir);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001528 fst_inode->i_ctime = time;
1529 snd_inode->i_ctime = time;
1530 old_dir->i_mtime = old_dir->i_ctime = time;
1531 new_dir->i_mtime = new_dir->i_ctime = time;
1532
1533 if (old_dir != new_dir) {
1534 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1535 inc_nlink(new_dir);
1536 drop_nlink(old_dir);
1537 }
1538 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1539 drop_nlink(new_dir);
1540 inc_nlink(old_dir);
1541 }
1542 }
1543
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001544 err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
1545 snd_inode, &snd_nm, sync);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001546
1547 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1548 ubifs_release_budget(c, &req);
1549
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001550 fscrypt_free_filename(&fst_nm);
1551 fscrypt_free_filename(&snd_nm);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001552 return err;
1553}
1554
Richard Weinberger390975a2016-10-18 21:21:43 +02001555static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
Richard Weinberger9ec64962016-09-14 22:28:51 +02001556 struct inode *new_dir, struct dentry *new_dentry,
1557 unsigned int flags)
1558{
Eric Biggers0c1ad522017-11-29 12:43:15 -08001559 int err;
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001560 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
Eric Biggers0c1ad522017-11-29 12:43:15 -08001561
Richard Weinberger9ec64962016-09-14 22:28:51 +02001562 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1563 return -EINVAL;
1564
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001565 ubifs_assert(c, inode_is_locked(old_dir));
1566 ubifs_assert(c, inode_is_locked(new_dir));
Richard Weinberger9ec64962016-09-14 22:28:51 +02001567
Eric Biggers0c1ad522017-11-29 12:43:15 -08001568 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1569 flags);
1570 if (err)
1571 return err;
1572
Richard Weinberger9ec64962016-09-14 22:28:51 +02001573 if (flags & RENAME_EXCHANGE)
1574 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1575
Richard Weinberger390975a2016-10-18 21:21:43 +02001576 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001577}
1578
David Howellsa528d352017-01-31 16:46:22 +00001579int ubifs_getattr(const struct path *path, struct kstat *stat,
1580 u32 request_mask, unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001581{
1582 loff_t size;
David Howellsa528d352017-01-31 16:46:22 +00001583 struct inode *inode = d_inode(path->dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001584 struct ubifs_inode *ui = ubifs_inode(inode);
1585
1586 mutex_lock(&ui->ui_mutex);
Richard Weinbergera02a6eb2017-05-21 00:16:26 +02001587
1588 if (ui->flags & UBIFS_APPEND_FL)
1589 stat->attributes |= STATX_ATTR_APPEND;
1590 if (ui->flags & UBIFS_COMPR_FL)
1591 stat->attributes |= STATX_ATTR_COMPRESSED;
1592 if (ui->flags & UBIFS_CRYPT_FL)
1593 stat->attributes |= STATX_ATTR_ENCRYPTED;
1594 if (ui->flags & UBIFS_IMMUTABLE_FL)
1595 stat->attributes |= STATX_ATTR_IMMUTABLE;
1596
1597 stat->attributes_mask |= (STATX_ATTR_APPEND |
1598 STATX_ATTR_COMPRESSED |
1599 STATX_ATTR_ENCRYPTED |
1600 STATX_ATTR_IMMUTABLE);
1601
Al Viro6d42e7e2012-04-02 14:25:07 -04001602 generic_fillattr(inode, stat);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001603 stat->blksize = UBIFS_BLOCK_SIZE;
1604 stat->size = ui->ui_size;
1605
1606 /*
1607 * Unfortunately, the 'stat()' system call was designed for block
1608 * device based file systems, and it is not appropriate for UBIFS,
1609 * because UBIFS does not have notion of "block". For example, it is
1610 * difficult to tell how many block a directory takes - it actually
1611 * takes less than 300 bytes, but we have to round it to block size,
1612 * which introduces large mistake. This makes utilities like 'du' to
1613 * report completely senseless numbers. This is the reason why UBIFS
1614 * goes the same way as JFFS2 - it reports zero blocks for everything
1615 * but regular files, which makes more sense than reporting completely
1616 * wrong sizes.
1617 */
1618 if (S_ISREG(inode->i_mode)) {
1619 size = ui->xattr_size;
1620 size += stat->size;
1621 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1622 /*
1623 * Note, user-space expects 512-byte blocks count irrespectively
1624 * of what was reported in @stat->size.
1625 */
1626 stat->blocks = size >> 9;
1627 } else
1628 stat->blocks = 0;
1629 mutex_unlock(&ui->ui_mutex);
1630 return 0;
1631}
1632
Richard Weinbergerba40e6a2016-09-29 17:20:27 +02001633static int ubifs_dir_open(struct inode *dir, struct file *file)
1634{
1635 if (ubifs_crypt_is_encrypted(dir))
1636 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1637
1638 return 0;
1639}
1640
Artem Bityutskiye8b81562009-01-15 17:43:23 +02001641const struct inode_operations ubifs_dir_inode_operations = {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001642 .lookup = ubifs_lookup,
1643 .create = ubifs_create,
1644 .link = ubifs_link,
1645 .symlink = ubifs_symlink,
1646 .unlink = ubifs_unlink,
1647 .mkdir = ubifs_mkdir,
1648 .rmdir = ubifs_rmdir,
1649 .mknod = ubifs_mknod,
Richard Weinberger390975a2016-10-18 21:21:43 +02001650 .rename = ubifs_rename,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001651 .setattr = ubifs_setattr,
1652 .getattr = ubifs_getattr,
Stefan Agner7e5471c2018-07-31 15:13:20 +02001653#ifdef CONFIG_UBIFS_FS_XATTR
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001654 .listxattr = ubifs_listxattr,
Stefan Agner7e5471c2018-07-31 15:13:20 +02001655#endif
Dongsheng Yang8c1c5f22015-11-07 12:46:11 +08001656#ifdef CONFIG_UBIFS_ATIME_SUPPORT
1657 .update_time = ubifs_update_time,
1658#endif
Richard Weinberger474b9372016-09-14 22:28:49 +02001659 .tmpfile = ubifs_tmpfile,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001660};
1661
Artem Bityutskiye8b81562009-01-15 17:43:23 +02001662const struct file_operations ubifs_dir_operations = {
Al Viro01122e02013-05-16 01:14:46 -04001663 .llseek = generic_file_llseek,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001664 .release = ubifs_dir_release,
1665 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001666 .iterate_shared = ubifs_readdir,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001667 .fsync = ubifs_fsync,
1668 .unlocked_ioctl = ubifs_ioctl,
Richard Weinbergerba40e6a2016-09-29 17:20:27 +02001669 .open = ubifs_dir_open,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001670#ifdef CONFIG_COMPAT
1671 .compat_ioctl = ubifs_compat_ioctl,
1672#endif
1673};