blob: 155521e51ac576e18e3169f44d617124e4428a40 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002/* * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
6 *
Artem Bityutskiy1e517642008-07-14 19:08:37 +03007 * Authors: Artem Bityutskiy (Битюцкий Артём)
8 * Adrian Hunter
9 * Zoltan Sogor
10 */
11
12/*
13 * This file implements directory operations.
14 *
15 * All FS operations in this file allocate budget before writing anything to the
16 * media. If they fail to allocate it, the error is returned. The only
17 * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
18 * if they unable to allocate the budget, because deletion %-ENOSPC failure is
19 * not what users are usually ready to get. UBIFS budgeting subsystem has some
20 * space reserved for these purposes.
21 *
22 * All operations in this file write all inodes which they change straight
23 * away, instead of marking them dirty. For example, 'ubifs_link()' changes
24 * @i_size of the parent inode and writes the parent inode together with the
25 * target inode. This was done to simplify file-system recovery which would
26 * otherwise be very difficult to do. The only exception is rename which marks
27 * the re-named inode dirty (because its @i_ctime is updated) but does not
28 * write it, but just marks it as dirty.
29 */
30
31#include "ubifs.h"
32
33/**
34 * inherit_flags - inherit flags of the parent inode.
35 * @dir: parent inode
36 * @mode: new inode mode flags
37 *
38 * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
39 * parent directory inode @dir. UBIFS inodes inherit the following flags:
40 * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
41 * sub-directory basis;
42 * o %UBIFS_SYNC_FL - useful for the same reasons;
43 * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
44 *
45 * This function returns the inherited flags.
46 */
Al Viroad44be52011-07-26 03:12:59 -040047static int inherit_flags(const struct inode *dir, umode_t mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030048{
49 int flags;
50 const struct ubifs_inode *ui = ubifs_inode(dir);
51
52 if (!S_ISDIR(dir->i_mode))
53 /*
54 * The parent is not a directory, which means that an extended
55 * attribute inode is being created. No flags.
56 */
57 return 0;
58
59 flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
60 if (!S_ISDIR(mode))
61 /* The "DIRSYNC" flag only applies to directories */
62 flags &= ~UBIFS_DIRSYNC_FL;
63 return flags;
64}
65
66/**
67 * ubifs_new_inode - allocate new UBIFS inode object.
68 * @c: UBIFS file-system description object
69 * @dir: parent directory inode
70 * @mode: inode mode flags
71 *
72 * This function finds an unused inode number, allocates new inode and
73 * initializes it. Returns new inode in case of success and an error code in
74 * case of failure.
75 */
Richard Weinbergerd475a502016-10-20 16:47:56 +020076struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
Al Viroad44be52011-07-26 03:12:59 -040077 umode_t mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030078{
Richard Weinbergerd475a502016-10-20 16:47:56 +020079 int err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030080 struct inode *inode;
81 struct ubifs_inode *ui;
Richard Weinbergerd475a502016-10-20 16:47:56 +020082 bool encrypted = false;
83
Artem Bityutskiy1e517642008-07-14 19:08:37 +030084 inode = new_inode(c->vfs_sb);
85 ui = ubifs_inode(inode);
86 if (!inode)
87 return ERR_PTR(-ENOMEM);
88
89 /*
90 * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
91 * marking them dirty in file write path (see 'file_update_time()').
92 * UBIFS has to fully control "clean <-> dirty" transitions of inodes
93 * to make budgeting work.
94 */
Artem Bityutskiy12e776a2011-05-27 15:50:39 +030095 inode->i_flags |= S_NOCMTIME;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030096
Dmitry Monakhovabf5d082010-03-04 17:32:21 +030097 inode_init_owner(inode, dir, mode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030098 inode->i_mtime = inode->i_atime = inode->i_ctime =
Deepa Dinamani607a11a2017-05-08 15:59:25 -070099 current_time(inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300100 inode->i_mapping->nrpages = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300101
Eric Biggers4c030fa2020-09-16 21:11:28 -0700102 err = fscrypt_prepare_new_inode(dir, inode, &encrypted);
103 if (err) {
104 ubifs_err(c, "fscrypt_prepare_new_inode failed: %i", err);
105 goto out_iput;
106 }
107
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300108 switch (mode & S_IFMT) {
109 case S_IFREG:
110 inode->i_mapping->a_ops = &ubifs_file_address_operations;
111 inode->i_op = &ubifs_file_inode_operations;
112 inode->i_fop = &ubifs_file_operations;
113 break;
114 case S_IFDIR:
115 inode->i_op = &ubifs_dir_inode_operations;
116 inode->i_fop = &ubifs_dir_operations;
117 inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
118 break;
119 case S_IFLNK:
120 inode->i_op = &ubifs_symlink_inode_operations;
121 break;
122 case S_IFSOCK:
123 case S_IFIFO:
124 case S_IFBLK:
125 case S_IFCHR:
126 inode->i_op = &ubifs_file_inode_operations;
127 break;
128 default:
129 BUG();
130 }
131
132 ui->flags = inherit_flags(dir, mode);
133 ubifs_set_inode_flags(inode);
134 if (S_ISREG(mode))
135 ui->compr_type = c->default_compr;
136 else
137 ui->compr_type = UBIFS_COMPR_NONE;
138 ui->synced_i_size = 0;
139
140 spin_lock(&c->cnt_lock);
141 /* Inode number overflow is currently not supported */
142 if (c->highest_inum >= INUM_WARN_WATERMARK) {
143 if (c->highest_inum >= INUM_WATERMARK) {
144 spin_unlock(&c->cnt_lock);
Sheng Yong235c3622015-03-20 10:39:42 +0000145 ubifs_err(c, "out of inode numbers");
Eric Biggers4c030fa2020-09-16 21:11:28 -0700146 err = -EINVAL;
147 goto out_iput;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300148 }
Sheng Yong1a7e985d2015-04-03 03:13:42 +0000149 ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200150 (unsigned long)c->highest_inum, INUM_WATERMARK);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300151 }
152
153 inode->i_ino = ++c->highest_inum;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300154 /*
155 * The creation sequence number remains with this inode for its
156 * lifetime. All nodes for this inode have a greater sequence number,
157 * and so it is possible to distinguish obsolete nodes belonging to a
158 * previous incarnation of the same inode number - for example, for the
159 * purpose of rebuilding the index.
160 */
161 ui->creat_sqnum = ++c->max_sqnum;
162 spin_unlock(&c->cnt_lock);
Richard Weinbergerd475a502016-10-20 16:47:56 +0200163
164 if (encrypted) {
Eric Biggers4c030fa2020-09-16 21:11:28 -0700165 err = fscrypt_set_context(inode, NULL);
Richard Weinbergerd475a502016-10-20 16:47:56 +0200166 if (err) {
Eric Biggers4c030fa2020-09-16 21:11:28 -0700167 ubifs_err(c, "fscrypt_set_context failed: %i", err);
168 goto out_iput;
Richard Weinbergerd475a502016-10-20 16:47:56 +0200169 }
170 }
171
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300172 return inode;
Eric Biggers4c030fa2020-09-16 21:11:28 -0700173
174out_iput:
175 make_bad_inode(inode);
176 iput(inode);
177 return ERR_PTR(err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300178}
179
Artem Bityutskiybb2615d2011-05-31 17:47:53 +0300180static int dbg_check_name(const struct ubifs_info *c,
181 const struct ubifs_dent_node *dent,
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100182 const struct fscrypt_name *nm)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300183{
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +0300184 if (!dbg_is_chk_gen(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300185 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100186 if (le16_to_cpu(dent->nlen) != fname_len(nm))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300187 return -EINVAL;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100188 if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300189 return -EINVAL;
190 return 0;
191}
192
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300193static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400194 unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300195{
196 int err;
197 union ubifs_key key;
198 struct inode *inode = NULL;
Al Viro191ac102018-04-30 20:14:59 -0400199 struct ubifs_dent_node *dent = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300200 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100201 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300202
Al Viro4cb2a012013-09-16 10:58:53 -0400203 dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300204
Eric Biggersb01531d2019-03-20 11:39:13 -0700205 err = fscrypt_prepare_lookup(dir, dentry, &nm);
206 if (err == -ENOENT)
207 return d_splice_alias(NULL, dentry);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100208 if (err)
209 return ERR_PTR(err);
210
211 if (fname_len(&nm) > UBIFS_MAX_NLEN) {
Al Viro191ac102018-04-30 20:14:59 -0400212 inode = ERR_PTR(-ENAMETOOLONG);
213 goto done;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100214 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300215
216 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100217 if (!dent) {
Al Viro191ac102018-04-30 20:14:59 -0400218 inode = ERR_PTR(-ENOMEM);
219 goto done;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100220 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300221
Eric Biggersaec992a2020-01-20 14:32:00 -0800222 if (fname_name(&nm) == NULL) {
Eric Biggersf0d07a92020-01-20 14:31:59 -0800223 if (nm.hash & ~UBIFS_S_KEY_HASH_MASK)
224 goto done; /* ENOENT */
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100225 dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
Richard Weinberger528e3d12016-11-11 20:46:06 +0100226 err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100227 } else {
228 dent_key_init(c, &key, dir->i_ino, &nm);
229 err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
230 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300231
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300232 if (err) {
Al Viro191ac102018-04-30 20:14:59 -0400233 if (err == -ENOENT)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300234 dbg_gen("not found");
Al Viro191ac102018-04-30 20:14:59 -0400235 else
236 inode = ERR_PTR(err);
237 goto done;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300238 }
239
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100240 if (dbg_check_name(c, dent, &nm)) {
Al Viro191ac102018-04-30 20:14:59 -0400241 inode = ERR_PTR(-EINVAL);
242 goto done;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300243 }
244
245 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
246 if (IS_ERR(inode)) {
247 /*
248 * This should not happen. Probably the file-system needs
249 * checking.
250 */
251 err = PTR_ERR(inode);
Sheng Yong235c3622015-03-20 10:39:42 +0000252 ubifs_err(c, "dead directory entry '%pd', error %d",
Al Viro4cb2a012013-09-16 10:58:53 -0400253 dentry, err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300254 ubifs_ro_mode(c, err);
Al Viro191ac102018-04-30 20:14:59 -0400255 goto done;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300256 }
257
Eric Biggers50d9fad2019-12-09 13:27:21 -0800258 if (IS_ENCRYPTED(dir) &&
Eric Biggers413d5a92017-04-07 10:58:40 -0700259 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
260 !fscrypt_has_permitted_context(dir, inode)) {
261 ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
262 dir->i_ino, inode->i_ino);
Al Viro191ac102018-04-30 20:14:59 -0400263 iput(inode);
264 inode = ERR_PTR(-EPERM);
Eric Biggers413d5a92017-04-07 10:58:40 -0700265 }
266
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300267done:
268 kfree(dent);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100269 fscrypt_free_filename(&nm);
Al Viro191ac102018-04-30 20:14:59 -0400270 return d_splice_alias(inode, dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300271}
272
Al Viro4acdaf22011-07-26 01:42:34 -0400273static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400274 bool excl)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300275{
276 struct inode *inode;
277 struct ubifs_info *c = dir->i_sb->s_fs_info;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300278 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
279 .dirtied_ino = 1 };
280 struct ubifs_inode *dir_ui = ubifs_inode(dir);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100281 struct fscrypt_name nm;
282 int err, sz_change;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300283
284 /*
285 * Budget request settings: new inode, new direntry, changing the
286 * parent directory inode.
287 */
288
Al Viro4cb2a012013-09-16 10:58:53 -0400289 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
290 dentry, mode, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300291
292 err = ubifs_budget_space(c, &req);
293 if (err)
294 return err;
295
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100296 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
297 if (err)
298 goto out_budg;
299
300 sz_change = CALC_DENT_SIZE(fname_len(&nm));
301
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300302 inode = ubifs_new_inode(c, dir, mode);
303 if (IS_ERR(inode)) {
304 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100305 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300306 }
307
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500308 err = ubifs_init_security(dir, inode, &dentry->d_name);
309 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +0100310 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500311
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300312 mutex_lock(&dir_ui->ui_mutex);
313 dir->i_size += sz_change;
314 dir_ui->ui_size = dir->i_size;
315 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100316 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300317 if (err)
318 goto out_cancel;
319 mutex_unlock(&dir_ui->ui_mutex);
320
321 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100322 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300323 insert_inode_hash(inode);
324 d_instantiate(dentry, inode);
325 return 0;
326
327out_cancel:
328 dir->i_size -= sz_change;
329 dir_ui->ui_size = dir->i_size;
330 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +0100331out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300332 make_bad_inode(inode);
333 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100334out_fname:
335 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300336out_budg:
337 ubifs_release_budget(c, &req);
Sheng Yong235c3622015-03-20 10:39:42 +0000338 ubifs_err(c, "cannot create regular file, error %d", err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300339 return err;
340}
341
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200342static int do_tmpfile(struct inode *dir, struct dentry *dentry,
343 umode_t mode, struct inode **whiteout)
Richard Weinberger474b9372016-09-14 22:28:49 +0200344{
345 struct inode *inode;
346 struct ubifs_info *c = dir->i_sb->s_fs_info;
347 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
348 struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
349 struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
350 int err, instantiated = 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100351 struct fscrypt_name nm;
Richard Weinberger474b9372016-09-14 22:28:49 +0200352
353 /*
354 * Budget request settings: new dirty inode, new direntry,
355 * budget for dirtied inode will be released via writeback.
356 */
357
358 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
359 dentry, mode, dir->i_ino);
360
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100361 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200362 if (err)
363 return err;
364
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100365 err = ubifs_budget_space(c, &req);
366 if (err) {
367 fscrypt_free_filename(&nm);
368 return err;
369 }
370
Richard Weinberger474b9372016-09-14 22:28:49 +0200371 err = ubifs_budget_space(c, &ino_req);
372 if (err) {
373 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100374 fscrypt_free_filename(&nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200375 return err;
376 }
377
378 inode = ubifs_new_inode(c, dir, mode);
379 if (IS_ERR(inode)) {
380 err = PTR_ERR(inode);
381 goto out_budg;
382 }
383 ui = ubifs_inode(inode);
384
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200385 if (whiteout) {
386 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200387 ubifs_assert(c, inode->i_op == &ubifs_file_inode_operations);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200388 }
389
Richard Weinberger474b9372016-09-14 22:28:49 +0200390 err = ubifs_init_security(dir, inode, &dentry->d_name);
391 if (err)
392 goto out_inode;
393
394 mutex_lock(&ui->ui_mutex);
395 insert_inode_hash(inode);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200396
397 if (whiteout) {
398 mark_inode_dirty(inode);
399 drop_nlink(inode);
400 *whiteout = inode;
401 } else {
402 d_tmpfile(dentry, inode);
403 }
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200404 ubifs_assert(c, ui->dirty);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200405
Richard Weinberger474b9372016-09-14 22:28:49 +0200406 instantiated = 1;
407 mutex_unlock(&ui->ui_mutex);
408
409 mutex_lock(&dir_ui->ui_mutex);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100410 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Richard Weinberger474b9372016-09-14 22:28:49 +0200411 if (err)
412 goto out_cancel;
413 mutex_unlock(&dir_ui->ui_mutex);
414
415 ubifs_release_budget(c, &req);
416
417 return 0;
418
419out_cancel:
420 mutex_unlock(&dir_ui->ui_mutex);
421out_inode:
422 make_bad_inode(inode);
423 if (!instantiated)
424 iput(inode);
425out_budg:
426 ubifs_release_budget(c, &req);
427 if (!instantiated)
428 ubifs_release_budget(c, &ino_req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100429 fscrypt_free_filename(&nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200430 ubifs_err(c, "cannot create temporary file, error %d", err);
431 return err;
432}
433
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200434static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
435 umode_t mode)
436{
437 return do_tmpfile(dir, dentry, mode, NULL);
438}
439
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300440/**
441 * vfs_dent_type - get VFS directory entry type.
442 * @type: UBIFS directory entry type
443 *
444 * This function converts UBIFS directory entry type into VFS directory entry
445 * type.
446 */
447static unsigned int vfs_dent_type(uint8_t type)
448{
449 switch (type) {
450 case UBIFS_ITYPE_REG:
451 return DT_REG;
452 case UBIFS_ITYPE_DIR:
453 return DT_DIR;
454 case UBIFS_ITYPE_LNK:
455 return DT_LNK;
456 case UBIFS_ITYPE_BLK:
457 return DT_BLK;
458 case UBIFS_ITYPE_CHR:
459 return DT_CHR;
460 case UBIFS_ITYPE_FIFO:
461 return DT_FIFO;
462 case UBIFS_ITYPE_SOCK:
463 return DT_SOCK;
464 default:
465 BUG();
466 }
467 return 0;
468}
469
470/*
471 * The classical Unix view for directory is that it is a linear array of
472 * (name, inode number) entries. Linux/VFS assumes this model as well.
473 * Particularly, 'readdir()' call wants us to return a directory entry offset
474 * which later may be used to continue 'readdir()'ing the directory or to
475 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
476 * model because directory entries are identified by keys, which may collide.
477 *
478 * UBIFS uses directory entry hash value for directory offsets, so
479 * 'seekdir()'/'telldir()' may not always work because of possible key
480 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
481 * properly by means of saving full directory entry name in the private field
482 * of the file description object.
483 *
484 * This means that UBIFS cannot support NFS which requires full
485 * 'seekdir()'/'telldir()' support.
486 */
Al Viro01122e02013-05-16 01:14:46 -0400487static int ubifs_readdir(struct file *file, struct dir_context *ctx)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300488{
Richard Weinbergerba75d572016-12-14 11:09:25 +0100489 int fstr_real_len = 0, err = 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100490 struct fscrypt_name nm;
491 struct fscrypt_str fstr = {0};
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300492 union ubifs_key key;
493 struct ubifs_dent_node *dent;
Al Viro496ad9a2013-01-23 17:07:38 -0500494 struct inode *dir = file_inode(file);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300495 struct ubifs_info *c = dir->i_sb->s_fs_info;
Eric Biggers50d9fad2019-12-09 13:27:21 -0800496 bool encrypted = IS_ENCRYPTED(dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300497
Al Viro01122e02013-05-16 01:14:46 -0400498 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300499
Al Viro01122e02013-05-16 01:14:46 -0400500 if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300501 /*
502 * The directory was seek'ed to a senseless position or there
503 * are no more entries.
504 */
505 return 0;
506
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100507 if (encrypted) {
508 err = fscrypt_get_encryption_info(dir);
Eric Biggers3b1ada552019-12-09 13:23:48 -0800509 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100510 return err;
511
Jeff Layton8b10fe62020-08-10 10:21:39 -0400512 err = fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN, &fstr);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100513 if (err)
514 return err;
515
516 fstr_real_len = fstr.len;
517 }
518
Artem Bityutskiy605c9122013-06-28 14:15:15 +0300519 if (file->f_version == 0) {
520 /*
521 * The file was seek'ed, which means that @file->private_data
522 * is now invalid. This may also be just the first
523 * 'ubifs_readdir()' invocation, in which case
524 * @file->private_data is NULL, and the below code is
525 * basically a no-op.
526 */
527 kfree(file->private_data);
528 file->private_data = NULL;
529 }
530
531 /*
532 * 'generic_file_llseek()' unconditionally sets @file->f_version to
533 * zero, and we use this for detecting whether the file was seek'ed.
534 */
535 file->f_version = 1;
536
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300537 /* File positions 0 and 1 correspond to "." and ".." */
Al Viro01122e02013-05-16 01:14:46 -0400538 if (ctx->pos < 2) {
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200539 ubifs_assert(c, !file->private_data);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100540 if (!dir_emit_dots(file, ctx)) {
541 if (encrypted)
542 fscrypt_fname_free_buffer(&fstr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300543 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100544 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300545
546 /* Find the first entry in TNC and save it */
547 lowest_dent_key(c, &key, dir->i_ino);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100548 fname_len(&nm) = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300549 dent = ubifs_tnc_next_ent(c, &key, &nm);
550 if (IS_ERR(dent)) {
551 err = PTR_ERR(dent);
552 goto out;
553 }
554
Al Viro01122e02013-05-16 01:14:46 -0400555 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300556 file->private_data = dent;
557 }
558
559 dent = file->private_data;
560 if (!dent) {
561 /*
562 * The directory was seek'ed to and is now readdir'ed.
Al Viro01122e02013-05-16 01:14:46 -0400563 * Find the entry corresponding to @ctx->pos or the closest one.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300564 */
Al Viro01122e02013-05-16 01:14:46 -0400565 dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100566 fname_len(&nm) = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300567 dent = ubifs_tnc_next_ent(c, &key, &nm);
568 if (IS_ERR(dent)) {
569 err = PTR_ERR(dent);
570 goto out;
571 }
Al Viro01122e02013-05-16 01:14:46 -0400572 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300573 file->private_data = dent;
574 }
575
576 while (1) {
Hyunchul Leeb20e2d92017-03-15 10:31:03 +0900577 dbg_gen("ino %llu, new f_pos %#x",
578 (unsigned long long)le64_to_cpu(dent->inum),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300579 key_hash_flash(c, &dent->key));
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200580 ubifs_assert(c, le64_to_cpu(dent->ch.sqnum) >
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700581 ubifs_inode(dir)->creat_sqnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300582
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100583 fname_len(&nm) = le16_to_cpu(dent->nlen);
584 fname_name(&nm) = dent->name;
585
586 if (encrypted) {
587 fstr.len = fstr_real_len;
588
Richard Weinberger528e3d12016-11-11 20:46:06 +0100589 err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
590 &dent->key),
591 le32_to_cpu(dent->cookie),
592 &nm.disk_name, &fstr);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +0200593 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100594 goto out;
595 } else {
596 fstr.len = fname_len(&nm);
597 fstr.name = fname_name(&nm);
598 }
599
600 if (!dir_emit(ctx, fstr.name, fstr.len,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300601 le64_to_cpu(dent->inum),
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100602 vfs_dent_type(dent->type))) {
603 if (encrypted)
604 fscrypt_fname_free_buffer(&fstr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300605 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100606 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300607
608 /* Switch to the next entry */
609 key_read(c, &dent->key, &key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300610 dent = ubifs_tnc_next_ent(c, &key, &nm);
611 if (IS_ERR(dent)) {
612 err = PTR_ERR(dent);
613 goto out;
614 }
615
616 kfree(file->private_data);
Al Viro01122e02013-05-16 01:14:46 -0400617 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300618 file->private_data = dent;
619 cond_resched();
620 }
621
622out:
Richard Weinbergeraeeb14f2015-10-12 23:35:36 +0200623 kfree(file->private_data);
624 file->private_data = NULL;
625
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100626 if (encrypted)
627 fscrypt_fname_free_buffer(&fstr);
628
Richard Weinbergerc83ed4c2016-10-19 12:43:07 +0200629 if (err != -ENOENT)
Sheng Yong235c3622015-03-20 10:39:42 +0000630 ubifs_err(c, "cannot find next direntry, error %d", err);
Richard Weinbergera00052a2016-10-28 11:49:03 +0200631 else
632 /*
633 * -ENOENT is a non-fatal error in this context, the TNC uses
634 * it to indicate that the cursor moved past the current directory
635 * and readdir() has to stop.
636 */
637 err = 0;
638
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300639
Artem Bityutskiy605c9122013-06-28 14:15:15 +0300640 /* 2 is a special value indicating that there are no more direntries */
Al Viro01122e02013-05-16 01:14:46 -0400641 ctx->pos = 2;
Richard Weinbergerc83ed4c2016-10-19 12:43:07 +0200642 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300643}
644
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300645/* Free saved readdir() state when the directory is closed */
646static int ubifs_dir_release(struct inode *dir, struct file *file)
647{
648 kfree(file->private_data);
649 file->private_data = NULL;
650 return 0;
651}
652
653/**
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200654 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300655 * @inode1: first inode
656 * @inode2: second inode
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200657 *
658 * We do not implement any tricks to guarantee strict lock ordering, because
659 * VFS has already done it for us on the @i_mutex. So this is just a simple
660 * wrapper function.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300661 */
662static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
663{
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200664 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
665 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300666}
667
668/**
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200669 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300670 * @inode1: first inode
671 * @inode2: second inode
672 */
673static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
674{
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300675 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200676 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300677}
678
679static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
680 struct dentry *dentry)
681{
682 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000683 struct inode *inode = d_inode(old_dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300684 struct ubifs_inode *ui = ubifs_inode(inode);
685 struct ubifs_inode *dir_ui = ubifs_inode(dir);
686 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
687 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +0300688 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100689 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300690
691 /*
692 * Budget request settings: new direntry, changing the target inode,
693 * changing the parent inode.
694 */
695
Al Viro4cb2a012013-09-16 10:58:53 -0400696 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
697 dentry, inode->i_ino,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300698 inode->i_nlink, dir->i_ino);
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200699 ubifs_assert(c, inode_is_locked(dir));
700 ubifs_assert(c, inode_is_locked(inode));
Hunter Adrian8b3884a2009-05-14 06:32:30 +0200701
Eric Biggers56538782017-11-29 12:43:14 -0800702 err = fscrypt_prepare_link(old_dentry, dir, dentry);
703 if (err)
704 return err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100705
706 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
707 if (err)
708 return err;
Richard Weinbergerac7e47a2016-09-29 20:00:38 +0200709
Artem Bityutskiyd808efb2011-05-31 18:14:38 +0300710 err = dbg_check_synced_i_size(c, inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300711 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100712 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300713
714 err = ubifs_budget_space(c, &req);
715 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100716 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300717
718 lock_2_inodes(dir, inode);
Richard Weinberger32fe9052017-03-30 10:50:49 +0200719
720 /* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
721 if (inode->i_nlink == 0)
722 ubifs_delete_orphan(c, inode->i_ino);
723
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300724 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400725 ihold(inode);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700726 inode->i_ctime = current_time(inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300727 dir->i_size += sz_change;
728 dir_ui->ui_size = dir->i_size;
729 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100730 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300731 if (err)
732 goto out_cancel;
733 unlock_2_inodes(dir, inode);
734
735 ubifs_release_budget(c, &req);
736 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100737 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300738 return 0;
739
740out_cancel:
741 dir->i_size -= sz_change;
742 dir_ui->ui_size = dir->i_size;
743 drop_nlink(inode);
Richard Weinberger32fe9052017-03-30 10:50:49 +0200744 if (inode->i_nlink == 0)
745 ubifs_add_orphan(c, inode->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300746 unlock_2_inodes(dir, inode);
747 ubifs_release_budget(c, &req);
748 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100749out_fname:
750 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300751 return err;
752}
753
754static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
755{
756 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000757 struct inode *inode = d_inode(dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300758 struct ubifs_inode *dir_ui = ubifs_inode(dir);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100759 int err, sz_change, budgeted = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300760 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200761 unsigned int saved_nlink = inode->i_nlink;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100762 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300763
764 /*
765 * Budget request settings: deletion direntry, deletion inode (+1 for
766 * @dirtied_ino), changing the parent directory inode. If budgeting
767 * fails, go ahead anyway because we have extra space reserved for
768 * deletions.
769 */
770
Al Viro4cb2a012013-09-16 10:58:53 -0400771 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
772 dentry, inode->i_ino,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300773 inode->i_nlink, dir->i_ino);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100774
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100775 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
776 if (err)
777 return err;
778
Richard Weinberger9ca2d732019-04-05 00:34:38 +0200779 err = ubifs_purge_xattrs(inode);
780 if (err)
781 return err;
782
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100783 sz_change = CALC_DENT_SIZE(fname_len(&nm));
784
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200785 ubifs_assert(c, inode_is_locked(dir));
786 ubifs_assert(c, inode_is_locked(inode));
Artem Bityutskiyd808efb2011-05-31 18:14:38 +0300787 err = dbg_check_synced_i_size(c, inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300788 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100789 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300790
791 err = ubifs_budget_space(c, &req);
792 if (err) {
793 if (err != -ENOSPC)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100794 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300795 budgeted = 0;
796 }
797
798 lock_2_inodes(dir, inode);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700799 inode->i_ctime = current_time(dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300800 drop_nlink(inode);
801 dir->i_size -= sz_change;
802 dir_ui->ui_size = dir->i_size;
803 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100804 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300805 if (err)
806 goto out_cancel;
807 unlock_2_inodes(dir, inode);
808
809 if (budgeted)
810 ubifs_release_budget(c, &req);
811 else {
812 /* We've deleted something - clean the "no space" flags */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300813 c->bi.nospace = c->bi.nospace_rp = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300814 smp_wmb();
815 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100816 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300817 return 0;
818
819out_cancel:
820 dir->i_size += sz_change;
821 dir_ui->ui_size = dir->i_size;
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200822 set_nlink(inode, saved_nlink);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300823 unlock_2_inodes(dir, inode);
824 if (budgeted)
825 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100826out_fname:
827 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300828 return err;
829}
830
831/**
832 * check_dir_empty - check if a directory is empty or not.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300833 * @dir: VFS inode object of the directory to check
834 *
835 * This function checks if directory @dir is empty. Returns zero if the
836 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
837 * in case of of errors.
838 */
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200839int ubifs_check_dir_empty(struct inode *dir)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300840{
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200841 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100842 struct fscrypt_name nm = { 0 };
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300843 struct ubifs_dent_node *dent;
844 union ubifs_key key;
845 int err;
846
847 lowest_dent_key(c, &key, dir->i_ino);
848 dent = ubifs_tnc_next_ent(c, &key, &nm);
849 if (IS_ERR(dent)) {
850 err = PTR_ERR(dent);
851 if (err == -ENOENT)
852 err = 0;
853 } else {
854 kfree(dent);
855 err = -ENOTEMPTY;
856 }
857 return err;
858}
859
860static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
861{
862 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000863 struct inode *inode = d_inode(dentry);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100864 int err, sz_change, budgeted = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300865 struct ubifs_inode *dir_ui = ubifs_inode(dir);
866 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100867 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300868
869 /*
870 * Budget request settings: deletion direntry, deletion inode and
871 * changing the parent inode. If budgeting fails, go ahead anyway
872 * because we have extra space reserved for deletions.
873 */
874
Al Viro4cb2a012013-09-16 10:58:53 -0400875 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
876 inode->i_ino, dir->i_ino);
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200877 ubifs_assert(c, inode_is_locked(dir));
878 ubifs_assert(c, inode_is_locked(inode));
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200879 err = ubifs_check_dir_empty(d_inode(dentry));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300880 if (err)
881 return err;
882
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100883 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
884 if (err)
885 return err;
886
Richard Weinberger9ca2d732019-04-05 00:34:38 +0200887 err = ubifs_purge_xattrs(inode);
888 if (err)
889 return err;
890
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100891 sz_change = CALC_DENT_SIZE(fname_len(&nm));
892
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300893 err = ubifs_budget_space(c, &req);
894 if (err) {
895 if (err != -ENOSPC)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100896 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300897 budgeted = 0;
898 }
899
900 lock_2_inodes(dir, inode);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700901 inode->i_ctime = current_time(dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300902 clear_nlink(inode);
903 drop_nlink(dir);
904 dir->i_size -= sz_change;
905 dir_ui->ui_size = dir->i_size;
906 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100907 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300908 if (err)
909 goto out_cancel;
910 unlock_2_inodes(dir, inode);
911
912 if (budgeted)
913 ubifs_release_budget(c, &req);
914 else {
915 /* We've deleted something - clean the "no space" flags */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300916 c->bi.nospace = c->bi.nospace_rp = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300917 smp_wmb();
918 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100919 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300920 return 0;
921
922out_cancel:
923 dir->i_size += sz_change;
924 dir_ui->ui_size = dir->i_size;
925 inc_nlink(dir);
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200926 set_nlink(inode, 2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300927 unlock_2_inodes(dir, inode);
928 if (budgeted)
929 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100930out_fname:
931 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300932 return err;
933}
934
Al Viro18bb1db2011-07-26 01:41:39 -0400935static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300936{
937 struct inode *inode;
938 struct ubifs_inode *dir_ui = ubifs_inode(dir);
939 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100940 int err, sz_change;
Artem Bityutskiy182854b2008-07-18 18:54:29 +0300941 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100942 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300943
944 /*
945 * Budget request settings: new inode, new direntry and changing parent
946 * directory inode.
947 */
948
Al Viro4cb2a012013-09-16 10:58:53 -0400949 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
950 dentry, mode, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300951
952 err = ubifs_budget_space(c, &req);
953 if (err)
954 return err;
955
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100956 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
957 if (err)
958 goto out_budg;
959
960 sz_change = CALC_DENT_SIZE(fname_len(&nm));
961
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300962 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
963 if (IS_ERR(inode)) {
964 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100965 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300966 }
967
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500968 err = ubifs_init_security(dir, inode, &dentry->d_name);
969 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +0100970 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500971
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300972 mutex_lock(&dir_ui->ui_mutex);
973 insert_inode_hash(inode);
974 inc_nlink(inode);
975 inc_nlink(dir);
976 dir->i_size += sz_change;
977 dir_ui->ui_size = dir->i_size;
978 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100979 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300980 if (err) {
Sheng Yong235c3622015-03-20 10:39:42 +0000981 ubifs_err(c, "cannot create directory, error %d", err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300982 goto out_cancel;
983 }
984 mutex_unlock(&dir_ui->ui_mutex);
985
986 ubifs_release_budget(c, &req);
987 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100988 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300989 return 0;
990
991out_cancel:
992 dir->i_size -= sz_change;
993 dir_ui->ui_size = dir->i_size;
994 drop_nlink(dir);
995 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +0100996out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300997 make_bad_inode(inode);
998 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100999out_fname:
1000 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001001out_budg:
1002 ubifs_release_budget(c, &req);
1003 return err;
1004}
1005
1006static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001007 umode_t mode, dev_t rdev)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001008{
1009 struct inode *inode;
1010 struct ubifs_inode *ui;
1011 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1012 struct ubifs_info *c = dir->i_sb->s_fs_info;
1013 union ubifs_dev_desc *dev = NULL;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001014 int sz_change;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001015 int err, devlen = 0;
1016 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001017 .dirtied_ino = 1 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001018 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001019
1020 /*
1021 * Budget request settings: new inode, new direntry and changing parent
1022 * directory inode.
1023 */
1024
Al Viro4cb2a012013-09-16 10:58:53 -04001025 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001026
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001027 if (S_ISBLK(mode) || S_ISCHR(mode)) {
1028 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1029 if (!dev)
1030 return -ENOMEM;
1031 devlen = ubifs_encode_dev(dev, rdev);
1032 }
1033
Hyunchul Lee4d35ca42017-05-17 08:57:18 +09001034 req.new_ino_d = ALIGN(devlen, 8);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001035 err = ubifs_budget_space(c, &req);
1036 if (err) {
1037 kfree(dev);
1038 return err;
1039 }
1040
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001041 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
Richard Weinberger63ed6572017-02-10 17:46:01 +01001042 if (err) {
1043 kfree(dev);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001044 goto out_budg;
Richard Weinberger63ed6572017-02-10 17:46:01 +01001045 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001046
1047 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1048
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001049 inode = ubifs_new_inode(c, dir, mode);
1050 if (IS_ERR(inode)) {
1051 kfree(dev);
1052 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001053 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001054 }
1055
1056 init_special_inode(inode, inode->i_mode, rdev);
1057 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
1058 ui = ubifs_inode(inode);
1059 ui->data = dev;
1060 ui->data_len = devlen;
1061
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001062 err = ubifs_init_security(dir, inode, &dentry->d_name);
1063 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001064 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001065
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001066 mutex_lock(&dir_ui->ui_mutex);
1067 dir->i_size += sz_change;
1068 dir_ui->ui_size = dir->i_size;
1069 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001070 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001071 if (err)
1072 goto out_cancel;
1073 mutex_unlock(&dir_ui->ui_mutex);
1074
1075 ubifs_release_budget(c, &req);
1076 insert_inode_hash(inode);
1077 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001078 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001079 return 0;
1080
1081out_cancel:
1082 dir->i_size -= sz_change;
1083 dir_ui->ui_size = dir->i_size;
1084 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +01001085out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001086 make_bad_inode(inode);
1087 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001088out_fname:
1089 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001090out_budg:
1091 ubifs_release_budget(c, &req);
1092 return err;
1093}
1094
1095static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
1096 const char *symname)
1097{
1098 struct inode *inode;
1099 struct ubifs_inode *ui;
1100 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1101 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinberger00ee8b62018-06-11 23:41:09 +02001102 int err, sz_change, len = strlen(symname);
Eric Biggers0e4dda22018-01-11 23:27:00 -05001103 struct fscrypt_str disk_link;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001104 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001105 .new_ino_d = ALIGN(len, 8),
1106 .dirtied_ino = 1 };
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001107 struct fscrypt_name nm;
1108
Eric Biggers0e4dda22018-01-11 23:27:00 -05001109 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
1110 symname, dir->i_ino);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001111
Eric Biggers0e4dda22018-01-11 23:27:00 -05001112 err = fscrypt_prepare_symlink(dir, symname, len, UBIFS_MAX_INO_DATA,
1113 &disk_link);
1114 if (err)
1115 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001116
1117 /*
1118 * Budget request settings: new inode, new direntry and changing parent
1119 * directory inode.
1120 */
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001121 err = ubifs_budget_space(c, &req);
1122 if (err)
1123 return err;
1124
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001125 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1126 if (err)
1127 goto out_budg;
1128
Richard Weinberger00ee8b62018-06-11 23:41:09 +02001129 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1130
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001131 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
1132 if (IS_ERR(inode)) {
1133 err = PTR_ERR(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001134 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001135 }
1136
1137 ui = ubifs_inode(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001138 ui->data = kmalloc(disk_link.len, GFP_NOFS);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001139 if (!ui->data) {
1140 err = -ENOMEM;
1141 goto out_inode;
1142 }
1143
Eric Biggers0e4dda22018-01-11 23:27:00 -05001144 if (IS_ENCRYPTED(inode)) {
1145 disk_link.name = ui->data; /* encrypt directly into ui->data */
1146 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
Eric Biggers6b46d442018-01-11 23:27:00 -05001147 if (err)
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001148 goto out_inode;
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001149 } else {
Eric Biggers0e4dda22018-01-11 23:27:00 -05001150 memcpy(ui->data, disk_link.name, disk_link.len);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001151 inode->i_link = ui->data;
1152 }
1153
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001154 /*
1155 * The terminating zero byte is not written to the flash media and it
1156 * is put just to make later in-memory string processing simpler. Thus,
Eric Biggers0e4dda22018-01-11 23:27:00 -05001157 * data length is @disk_link.len - 1, not @disk_link.len.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001158 */
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001159 ui->data_len = disk_link.len - 1;
1160 inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001161
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001162 err = ubifs_init_security(dir, inode, &dentry->d_name);
1163 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001164 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001165
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001166 mutex_lock(&dir_ui->ui_mutex);
1167 dir->i_size += sz_change;
1168 dir_ui->ui_size = dir->i_size;
1169 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001170 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001171 if (err)
1172 goto out_cancel;
1173 mutex_unlock(&dir_ui->ui_mutex);
1174
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001175 insert_inode_hash(inode);
1176 d_instantiate(dentry, inode);
Eric Biggers6b46d442018-01-11 23:27:00 -05001177 err = 0;
1178 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001179
1180out_cancel:
1181 dir->i_size -= sz_change;
1182 dir_ui->ui_size = dir->i_size;
1183 mutex_unlock(&dir_ui->ui_mutex);
1184out_inode:
1185 make_bad_inode(inode);
1186 iput(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001187out_fname:
1188 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001189out_budg:
1190 ubifs_release_budget(c, &req);
1191 return err;
1192}
1193
1194/**
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001195 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001196 * @inode1: first inode
1197 * @inode2: second inode
1198 * @inode3: third inode
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001199 * @inode4: fouth inode
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001200 *
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001201 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001202 * @inode2 whereas @inode3 and @inode4 may be %NULL.
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001203 *
1204 * We do not implement any tricks to guarantee strict lock ordering, because
1205 * VFS has already done it for us on the @i_mutex. So this is just a simple
1206 * wrapper function.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001207 */
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001208static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1209 struct inode *inode3, struct inode *inode4)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001210{
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001211 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1212 if (inode2 != inode1)
1213 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1214 if (inode3)
1215 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001216 if (inode4)
1217 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001218}
1219
1220/**
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001221 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001222 * @inode1: first inode
1223 * @inode2: second inode
1224 * @inode3: third inode
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001225 * @inode4: fouth inode
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001226 */
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001227static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1228 struct inode *inode3, struct inode *inode4)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001229{
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001230 if (inode4)
1231 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001232 if (inode3)
1233 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001234 if (inode1 != inode2)
1235 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1236 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001237}
1238
Richard Weinberger390975a2016-10-18 21:21:43 +02001239static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1240 struct inode *new_dir, struct dentry *new_dentry,
1241 unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001242{
1243 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +00001244 struct inode *old_inode = d_inode(old_dentry);
1245 struct inode *new_inode = d_inode(new_dentry);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001246 struct inode *whiteout = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001247 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001248 struct ubifs_inode *whiteout_ui = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001249 int err, release, sync = 0, move = (new_dir != old_dir);
1250 int is_dir = S_ISDIR(old_inode->i_mode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001251 int unlink = !!new_inode, new_sz, old_sz;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001252 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1253 .dirtied_ino = 3 };
1254 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001255 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
Deepa Dinamani95582b02018-05-08 19:36:02 -07001256 struct timespec64 time;
Kees Cook3f649ab2020-06-03 13:09:38 -07001257 unsigned int saved_nlink;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001258 struct fscrypt_name old_nm, new_nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001259
1260 /*
1261 * Budget request settings: deletion direntry, new direntry, removing
1262 * the old inode, and changing old and new parent directory inodes.
1263 *
1264 * However, this operation also marks the target inode as dirty and
1265 * does not write it, so we allocate budget for the target inode
1266 * separately.
1267 */
1268
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001269 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 -04001270 old_dentry, old_inode->i_ino, old_dir->i_ino,
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001271 new_dentry, new_dir->i_ino, flags);
1272
Richard Weinberger9ca2d732019-04-05 00:34:38 +02001273 if (unlink) {
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001274 ubifs_assert(c, inode_is_locked(new_inode));
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001275
Richard Weinberger9ca2d732019-04-05 00:34:38 +02001276 err = ubifs_purge_xattrs(new_inode);
1277 if (err)
1278 return err;
1279 }
1280
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001281 if (unlink && is_dir) {
Richard Weinbergerf6337d82016-09-19 20:54:19 +02001282 err = ubifs_check_dir_empty(new_inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001283 if (err)
1284 return err;
1285 }
1286
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001287 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001288 if (err)
1289 return err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001290
1291 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
1292 if (err) {
1293 fscrypt_free_filename(&old_nm);
1294 return err;
1295 }
1296
1297 new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
1298 old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
1299
1300 err = ubifs_budget_space(c, &req);
1301 if (err) {
1302 fscrypt_free_filename(&old_nm);
1303 fscrypt_free_filename(&new_nm);
1304 return err;
1305 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001306 err = ubifs_budget_space(c, &ino_req);
1307 if (err) {
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001308 fscrypt_free_filename(&old_nm);
1309 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001310 ubifs_release_budget(c, &req);
1311 return err;
1312 }
1313
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001314 if (flags & RENAME_WHITEOUT) {
1315 union ubifs_dev_desc *dev = NULL;
1316
1317 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1318 if (!dev) {
Hyunchul Leebb50c632017-05-17 08:58:02 +09001319 err = -ENOMEM;
1320 goto out_release;
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001321 }
1322
1323 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1324 if (err) {
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001325 kfree(dev);
Hyunchul Leebb50c632017-05-17 08:58:02 +09001326 goto out_release;
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001327 }
1328
1329 whiteout->i_state |= I_LINKABLE;
1330 whiteout_ui = ubifs_inode(whiteout);
1331 whiteout_ui->data = dev;
1332 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001333 ubifs_assert(c, !whiteout_ui->dirty);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001334 }
1335
1336 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001337
1338 /*
1339 * Like most other Unix systems, set the @i_ctime for inodes on a
1340 * rename.
1341 */
Deepa Dinamani607a11a2017-05-08 15:59:25 -07001342 time = current_time(old_dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001343 old_inode->i_ctime = time;
1344
1345 /* We must adjust parent link count when renaming directories */
1346 if (is_dir) {
1347 if (move) {
1348 /*
1349 * @old_dir loses a link because we are moving
1350 * @old_inode to a different directory.
1351 */
1352 drop_nlink(old_dir);
1353 /*
1354 * @new_dir only gains a link if we are not also
1355 * overwriting an existing directory.
1356 */
1357 if (!unlink)
1358 inc_nlink(new_dir);
1359 } else {
1360 /*
1361 * @old_inode is not moving to a different directory,
1362 * but @old_dir still loses a link if we are
1363 * overwriting an existing directory.
1364 */
1365 if (unlink)
1366 drop_nlink(old_dir);
1367 }
1368 }
1369
1370 old_dir->i_size -= old_sz;
1371 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1372 old_dir->i_mtime = old_dir->i_ctime = time;
1373 new_dir->i_mtime = new_dir->i_ctime = time;
1374
1375 /*
1376 * And finally, if we unlinked a direntry which happened to have the
1377 * same name as the moved direntry, we have to decrement @i_nlink of
1378 * the unlinked inode and change its ctime.
1379 */
1380 if (unlink) {
1381 /*
1382 * Directories cannot have hard-links, so if this is a
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001383 * directory, just clear @i_nlink.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001384 */
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001385 saved_nlink = new_inode->i_nlink;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001386 if (is_dir)
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001387 clear_nlink(new_inode);
1388 else
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001389 drop_nlink(new_inode);
1390 new_inode->i_ctime = time;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001391 } else {
1392 new_dir->i_size += new_sz;
1393 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1394 }
1395
1396 /*
1397 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1398 * is dirty, because this will be done later on at the end of
1399 * 'ubifs_rename()'.
1400 */
1401 if (IS_SYNC(old_inode)) {
1402 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1403 if (unlink && IS_SYNC(new_inode))
1404 sync = 1;
1405 }
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001406
1407 if (whiteout) {
1408 struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1409 .dirtied_ino_d = \
1410 ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1411
1412 err = ubifs_budget_space(c, &wht_req);
1413 if (err) {
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001414 kfree(whiteout_ui->data);
1415 whiteout_ui->data_len = 0;
1416 iput(whiteout);
Hyunchul Leebb50c632017-05-17 08:58:02 +09001417 goto out_release;
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001418 }
1419
1420 inc_nlink(whiteout);
1421 mark_inode_dirty(whiteout);
1422 whiteout->i_state &= ~I_LINKABLE;
1423 iput(whiteout);
1424 }
1425
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001426 err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
1427 new_inode, &new_nm, whiteout, sync);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001428 if (err)
1429 goto out_cancel;
1430
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001431 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001432 ubifs_release_budget(c, &req);
1433
1434 mutex_lock(&old_inode_ui->ui_mutex);
1435 release = old_inode_ui->dirty;
1436 mark_inode_dirty_sync(old_inode);
1437 mutex_unlock(&old_inode_ui->ui_mutex);
1438
1439 if (release)
1440 ubifs_release_budget(c, &ino_req);
1441 if (IS_SYNC(old_inode))
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001442 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001443
1444 fscrypt_free_filename(&old_nm);
1445 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001446 return err;
1447
1448out_cancel:
1449 if (unlink) {
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001450 set_nlink(new_inode, saved_nlink);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001451 } else {
1452 new_dir->i_size -= new_sz;
1453 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1454 }
1455 old_dir->i_size += old_sz;
1456 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1457 if (is_dir) {
1458 if (move) {
1459 inc_nlink(old_dir);
1460 if (!unlink)
1461 drop_nlink(new_dir);
1462 } else {
1463 if (unlink)
1464 inc_nlink(old_dir);
1465 }
1466 }
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001467 if (whiteout) {
1468 drop_nlink(whiteout);
1469 iput(whiteout);
1470 }
1471 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Hyunchul Leebb50c632017-05-17 08:58:02 +09001472out_release:
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001473 ubifs_release_budget(c, &ino_req);
1474 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001475 fscrypt_free_filename(&old_nm);
1476 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001477 return err;
1478}
1479
Richard Weinberger9ec64962016-09-14 22:28:51 +02001480static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1481 struct inode *new_dir, struct dentry *new_dentry)
1482{
1483 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1484 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1485 .dirtied_ino = 2 };
1486 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1487 struct inode *fst_inode = d_inode(old_dentry);
1488 struct inode *snd_inode = d_inode(new_dentry);
Deepa Dinamani95582b02018-05-08 19:36:02 -07001489 struct timespec64 time;
Richard Weinberger9ec64962016-09-14 22:28:51 +02001490 int err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001491 struct fscrypt_name fst_nm, snd_nm;
Richard Weinberger9ec64962016-09-14 22:28:51 +02001492
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001493 ubifs_assert(c, fst_inode && snd_inode);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001494
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001495 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
1496 if (err)
1497 return err;
1498
1499 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
1500 if (err) {
1501 fscrypt_free_filename(&fst_nm);
1502 return err;
1503 }
1504
Richard Weinberger9ec64962016-09-14 22:28:51 +02001505 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1506
Deepa Dinamani607a11a2017-05-08 15:59:25 -07001507 time = current_time(old_dir);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001508 fst_inode->i_ctime = time;
1509 snd_inode->i_ctime = time;
1510 old_dir->i_mtime = old_dir->i_ctime = time;
1511 new_dir->i_mtime = new_dir->i_ctime = time;
1512
1513 if (old_dir != new_dir) {
1514 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1515 inc_nlink(new_dir);
1516 drop_nlink(old_dir);
1517 }
1518 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1519 drop_nlink(new_dir);
1520 inc_nlink(old_dir);
1521 }
1522 }
1523
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001524 err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
1525 snd_inode, &snd_nm, sync);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001526
1527 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1528 ubifs_release_budget(c, &req);
1529
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001530 fscrypt_free_filename(&fst_nm);
1531 fscrypt_free_filename(&snd_nm);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001532 return err;
1533}
1534
Richard Weinberger390975a2016-10-18 21:21:43 +02001535static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
Richard Weinberger9ec64962016-09-14 22:28:51 +02001536 struct inode *new_dir, struct dentry *new_dentry,
1537 unsigned int flags)
1538{
Eric Biggers0c1ad522017-11-29 12:43:15 -08001539 int err;
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001540 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
Eric Biggers0c1ad522017-11-29 12:43:15 -08001541
Richard Weinberger9ec64962016-09-14 22:28:51 +02001542 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1543 return -EINVAL;
1544
Richard Weinberger6eb61d52018-07-12 13:01:57 +02001545 ubifs_assert(c, inode_is_locked(old_dir));
1546 ubifs_assert(c, inode_is_locked(new_dir));
Richard Weinberger9ec64962016-09-14 22:28:51 +02001547
Eric Biggers0c1ad522017-11-29 12:43:15 -08001548 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1549 flags);
1550 if (err)
1551 return err;
1552
Richard Weinberger9ec64962016-09-14 22:28:51 +02001553 if (flags & RENAME_EXCHANGE)
1554 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1555
Richard Weinberger390975a2016-10-18 21:21:43 +02001556 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001557}
1558
David Howellsa528d352017-01-31 16:46:22 +00001559int ubifs_getattr(const struct path *path, struct kstat *stat,
1560 u32 request_mask, unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001561{
1562 loff_t size;
David Howellsa528d352017-01-31 16:46:22 +00001563 struct inode *inode = d_inode(path->dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001564 struct ubifs_inode *ui = ubifs_inode(inode);
1565
1566 mutex_lock(&ui->ui_mutex);
Richard Weinbergera02a6eb2017-05-21 00:16:26 +02001567
1568 if (ui->flags & UBIFS_APPEND_FL)
1569 stat->attributes |= STATX_ATTR_APPEND;
1570 if (ui->flags & UBIFS_COMPR_FL)
1571 stat->attributes |= STATX_ATTR_COMPRESSED;
1572 if (ui->flags & UBIFS_CRYPT_FL)
1573 stat->attributes |= STATX_ATTR_ENCRYPTED;
1574 if (ui->flags & UBIFS_IMMUTABLE_FL)
1575 stat->attributes |= STATX_ATTR_IMMUTABLE;
1576
1577 stat->attributes_mask |= (STATX_ATTR_APPEND |
1578 STATX_ATTR_COMPRESSED |
1579 STATX_ATTR_ENCRYPTED |
1580 STATX_ATTR_IMMUTABLE);
1581
Al Viro6d42e7e2012-04-02 14:25:07 -04001582 generic_fillattr(inode, stat);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001583 stat->blksize = UBIFS_BLOCK_SIZE;
1584 stat->size = ui->ui_size;
1585
1586 /*
1587 * Unfortunately, the 'stat()' system call was designed for block
1588 * device based file systems, and it is not appropriate for UBIFS,
1589 * because UBIFS does not have notion of "block". For example, it is
1590 * difficult to tell how many block a directory takes - it actually
1591 * takes less than 300 bytes, but we have to round it to block size,
1592 * which introduces large mistake. This makes utilities like 'du' to
1593 * report completely senseless numbers. This is the reason why UBIFS
1594 * goes the same way as JFFS2 - it reports zero blocks for everything
1595 * but regular files, which makes more sense than reporting completely
1596 * wrong sizes.
1597 */
1598 if (S_ISREG(inode->i_mode)) {
1599 size = ui->xattr_size;
1600 size += stat->size;
1601 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1602 /*
1603 * Note, user-space expects 512-byte blocks count irrespectively
1604 * of what was reported in @stat->size.
1605 */
1606 stat->blocks = size >> 9;
1607 } else
1608 stat->blocks = 0;
1609 mutex_unlock(&ui->ui_mutex);
1610 return 0;
1611}
1612
Richard Weinbergerba40e6a2016-09-29 17:20:27 +02001613static int ubifs_dir_open(struct inode *dir, struct file *file)
1614{
Eric Biggers50d9fad2019-12-09 13:27:21 -08001615 if (IS_ENCRYPTED(dir))
Richard Weinbergerba40e6a2016-09-29 17:20:27 +02001616 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1617
1618 return 0;
1619}
1620
Artem Bityutskiye8b81562009-01-15 17:43:23 +02001621const struct inode_operations ubifs_dir_inode_operations = {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001622 .lookup = ubifs_lookup,
1623 .create = ubifs_create,
1624 .link = ubifs_link,
1625 .symlink = ubifs_symlink,
1626 .unlink = ubifs_unlink,
1627 .mkdir = ubifs_mkdir,
1628 .rmdir = ubifs_rmdir,
1629 .mknod = ubifs_mknod,
Richard Weinberger390975a2016-10-18 21:21:43 +02001630 .rename = ubifs_rename,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001631 .setattr = ubifs_setattr,
1632 .getattr = ubifs_getattr,
Stefan Agner7e5471c2018-07-31 15:13:20 +02001633#ifdef CONFIG_UBIFS_FS_XATTR
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001634 .listxattr = ubifs_listxattr,
Stefan Agner7e5471c2018-07-31 15:13:20 +02001635#endif
Dongsheng Yang8c1c5f22015-11-07 12:46:11 +08001636 .update_time = ubifs_update_time,
Richard Weinberger474b9372016-09-14 22:28:49 +02001637 .tmpfile = ubifs_tmpfile,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001638};
1639
Artem Bityutskiye8b81562009-01-15 17:43:23 +02001640const struct file_operations ubifs_dir_operations = {
Al Viro01122e02013-05-16 01:14:46 -04001641 .llseek = generic_file_llseek,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001642 .release = ubifs_dir_release,
1643 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001644 .iterate_shared = ubifs_readdir,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001645 .fsync = ubifs_fsync,
1646 .unlocked_ioctl = ubifs_ioctl,
Richard Weinbergerba40e6a2016-09-29 17:20:27 +02001647 .open = ubifs_dir_open,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001648#ifdef CONFIG_COMPAT
1649 .compat_ioctl = ubifs_compat_ioctl,
1650#endif
1651};