blob: bbe2b346a94f2a2e7fd09a00e78e26b4cae11af6 [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 =
124 ubifs_current_time(inode);
125 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;
146 break;
147 default:
148 BUG();
149 }
150
151 ui->flags = inherit_flags(dir, mode);
152 ubifs_set_inode_flags(inode);
153 if (S_ISREG(mode))
154 ui->compr_type = c->default_compr;
155 else
156 ui->compr_type = UBIFS_COMPR_NONE;
157 ui->synced_i_size = 0;
158
159 spin_lock(&c->cnt_lock);
160 /* Inode number overflow is currently not supported */
161 if (c->highest_inum >= INUM_WARN_WATERMARK) {
162 if (c->highest_inum >= INUM_WATERMARK) {
163 spin_unlock(&c->cnt_lock);
Sheng Yong235c3622015-03-20 10:39:42 +0000164 ubifs_err(c, "out of inode numbers");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300165 make_bad_inode(inode);
166 iput(inode);
167 return ERR_PTR(-EINVAL);
168 }
Sheng Yong1a7e985d2015-04-03 03:13:42 +0000169 ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200170 (unsigned long)c->highest_inum, INUM_WATERMARK);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300171 }
172
173 inode->i_ino = ++c->highest_inum;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300174 /*
175 * The creation sequence number remains with this inode for its
176 * lifetime. All nodes for this inode have a greater sequence number,
177 * and so it is possible to distinguish obsolete nodes belonging to a
178 * previous incarnation of the same inode number - for example, for the
179 * purpose of rebuilding the index.
180 */
181 ui->creat_sqnum = ++c->max_sqnum;
182 spin_unlock(&c->cnt_lock);
Richard Weinbergerd475a502016-10-20 16:47:56 +0200183
184 if (encrypted) {
185 err = fscrypt_inherit_context(dir, inode, &encrypted, true);
186 if (err) {
187 ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
188 make_bad_inode(inode);
189 iput(inode);
190 return ERR_PTR(err);
191 }
192 }
193
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300194 return inode;
195}
196
Artem Bityutskiybb2615d2011-05-31 17:47:53 +0300197static int dbg_check_name(const struct ubifs_info *c,
198 const struct ubifs_dent_node *dent,
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100199 const struct fscrypt_name *nm)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300200{
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +0300201 if (!dbg_is_chk_gen(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300202 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100203 if (le16_to_cpu(dent->nlen) != fname_len(nm))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300204 return -EINVAL;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100205 if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300206 return -EINVAL;
207 return 0;
208}
209
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300210static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400211 unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300212{
213 int err;
214 union ubifs_key key;
215 struct inode *inode = NULL;
216 struct ubifs_dent_node *dent;
217 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100218 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300219
Al Viro4cb2a012013-09-16 10:58:53 -0400220 dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300221
Richard Weinberger9270b2f2016-09-29 20:10:06 +0200222 if (ubifs_crypt_is_encrypted(dir)) {
223 err = fscrypt_get_encryption_info(dir);
224
225 /*
226 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
227 * created while the directory was encrypted and we
228 * have access to the key.
229 */
230 if (fscrypt_has_encryption_key(dir))
231 fscrypt_set_encrypted_dentry(dentry);
232 fscrypt_set_d_op(dentry);
233 if (err && err != -ENOKEY)
234 return ERR_PTR(err);
235 }
236
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100237 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
238 if (err)
239 return ERR_PTR(err);
240
241 if (fname_len(&nm) > UBIFS_MAX_NLEN) {
242 err = -ENAMETOOLONG;
243 goto out_fname;
244 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300245
246 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100247 if (!dent) {
248 err = -ENOMEM;
249 goto out_fname;
250 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300251
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100252 if (nm.hash) {
253 ubifs_assert(fname_len(&nm) == 0);
254 ubifs_assert(fname_name(&nm) == NULL);
255 dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
Richard Weinberger528e3d12016-11-11 20:46:06 +0100256 err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100257 } else {
258 dent_key_init(c, &key, dir->i_ino, &nm);
259 err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
260 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300261
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300262 if (err) {
Artem Bityutskiy720b4992008-08-13 16:16:31 +0300263 if (err == -ENOENT) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300264 dbg_gen("not found");
265 goto done;
266 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100267 goto out_dent;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300268 }
269
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100270 if (dbg_check_name(c, dent, &nm)) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300271 err = -EINVAL;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100272 goto out_dent;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300273 }
274
275 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
276 if (IS_ERR(inode)) {
277 /*
278 * This should not happen. Probably the file-system needs
279 * checking.
280 */
281 err = PTR_ERR(inode);
Sheng Yong235c3622015-03-20 10:39:42 +0000282 ubifs_err(c, "dead directory entry '%pd', error %d",
Al Viro4cb2a012013-09-16 10:58:53 -0400283 dentry, err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300284 ubifs_ro_mode(c, err);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100285 goto out_dent;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300286 }
287
Eric Biggers413d5a92017-04-07 10:58:40 -0700288 if (ubifs_crypt_is_encrypted(dir) &&
289 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
290 !fscrypt_has_permitted_context(dir, inode)) {
291 ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
292 dir->i_ino, inode->i_ino);
293 err = -EPERM;
294 goto out_inode;
295 }
296
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300297done:
298 kfree(dent);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100299 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300300 /*
301 * Note, d_splice_alias() would be required instead if we supported
302 * NFS.
303 */
304 d_add(dentry, inode);
305 return NULL;
306
Eric Biggers413d5a92017-04-07 10:58:40 -0700307out_inode:
308 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100309out_dent:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300310 kfree(dent);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100311out_fname:
312 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300313 return ERR_PTR(err);
314}
315
Al Viro4acdaf22011-07-26 01:42:34 -0400316static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400317 bool excl)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300318{
319 struct inode *inode;
320 struct ubifs_info *c = dir->i_sb->s_fs_info;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300321 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
322 .dirtied_ino = 1 };
323 struct ubifs_inode *dir_ui = ubifs_inode(dir);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100324 struct fscrypt_name nm;
325 int err, sz_change;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300326
327 /*
328 * Budget request settings: new inode, new direntry, changing the
329 * parent directory inode.
330 */
331
Al Viro4cb2a012013-09-16 10:58:53 -0400332 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
333 dentry, mode, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300334
335 err = ubifs_budget_space(c, &req);
336 if (err)
337 return err;
338
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100339 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
340 if (err)
341 goto out_budg;
342
343 sz_change = CALC_DENT_SIZE(fname_len(&nm));
344
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300345 inode = ubifs_new_inode(c, dir, mode);
346 if (IS_ERR(inode)) {
347 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100348 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300349 }
350
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500351 err = ubifs_init_security(dir, inode, &dentry->d_name);
352 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +0100353 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500354
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300355 mutex_lock(&dir_ui->ui_mutex);
356 dir->i_size += sz_change;
357 dir_ui->ui_size = dir->i_size;
358 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100359 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300360 if (err)
361 goto out_cancel;
362 mutex_unlock(&dir_ui->ui_mutex);
363
364 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100365 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300366 insert_inode_hash(inode);
367 d_instantiate(dentry, inode);
368 return 0;
369
370out_cancel:
371 dir->i_size -= sz_change;
372 dir_ui->ui_size = dir->i_size;
373 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +0100374out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300375 make_bad_inode(inode);
376 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100377out_fname:
378 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300379out_budg:
380 ubifs_release_budget(c, &req);
Sheng Yong235c3622015-03-20 10:39:42 +0000381 ubifs_err(c, "cannot create regular file, error %d", err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300382 return err;
383}
384
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200385static int do_tmpfile(struct inode *dir, struct dentry *dentry,
386 umode_t mode, struct inode **whiteout)
Richard Weinberger474b9372016-09-14 22:28:49 +0200387{
388 struct inode *inode;
389 struct ubifs_info *c = dir->i_sb->s_fs_info;
390 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
391 struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
392 struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
393 int err, instantiated = 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100394 struct fscrypt_name nm;
Richard Weinberger474b9372016-09-14 22:28:49 +0200395
396 /*
397 * Budget request settings: new dirty inode, new direntry,
398 * budget for dirtied inode will be released via writeback.
399 */
400
401 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
402 dentry, mode, dir->i_ino);
403
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100404 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200405 if (err)
406 return err;
407
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100408 err = ubifs_budget_space(c, &req);
409 if (err) {
410 fscrypt_free_filename(&nm);
411 return err;
412 }
413
Richard Weinberger474b9372016-09-14 22:28:49 +0200414 err = ubifs_budget_space(c, &ino_req);
415 if (err) {
416 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100417 fscrypt_free_filename(&nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200418 return err;
419 }
420
421 inode = ubifs_new_inode(c, dir, mode);
422 if (IS_ERR(inode)) {
423 err = PTR_ERR(inode);
424 goto out_budg;
425 }
426 ui = ubifs_inode(inode);
427
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200428 if (whiteout) {
429 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
430 ubifs_assert(inode->i_op == &ubifs_file_inode_operations);
431 }
432
Richard Weinberger474b9372016-09-14 22:28:49 +0200433 err = ubifs_init_security(dir, inode, &dentry->d_name);
434 if (err)
435 goto out_inode;
436
437 mutex_lock(&ui->ui_mutex);
438 insert_inode_hash(inode);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200439
440 if (whiteout) {
441 mark_inode_dirty(inode);
442 drop_nlink(inode);
443 *whiteout = inode;
444 } else {
445 d_tmpfile(dentry, inode);
446 }
Richard Weinberger474b9372016-09-14 22:28:49 +0200447 ubifs_assert(ui->dirty);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200448
Richard Weinberger474b9372016-09-14 22:28:49 +0200449 instantiated = 1;
450 mutex_unlock(&ui->ui_mutex);
451
452 mutex_lock(&dir_ui->ui_mutex);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100453 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Richard Weinberger474b9372016-09-14 22:28:49 +0200454 if (err)
455 goto out_cancel;
456 mutex_unlock(&dir_ui->ui_mutex);
457
458 ubifs_release_budget(c, &req);
459
460 return 0;
461
462out_cancel:
463 mutex_unlock(&dir_ui->ui_mutex);
464out_inode:
465 make_bad_inode(inode);
466 if (!instantiated)
467 iput(inode);
468out_budg:
469 ubifs_release_budget(c, &req);
470 if (!instantiated)
471 ubifs_release_budget(c, &ino_req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100472 fscrypt_free_filename(&nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200473 ubifs_err(c, "cannot create temporary file, error %d", err);
474 return err;
475}
476
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200477static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
478 umode_t mode)
479{
480 return do_tmpfile(dir, dentry, mode, NULL);
481}
482
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300483/**
484 * vfs_dent_type - get VFS directory entry type.
485 * @type: UBIFS directory entry type
486 *
487 * This function converts UBIFS directory entry type into VFS directory entry
488 * type.
489 */
490static unsigned int vfs_dent_type(uint8_t type)
491{
492 switch (type) {
493 case UBIFS_ITYPE_REG:
494 return DT_REG;
495 case UBIFS_ITYPE_DIR:
496 return DT_DIR;
497 case UBIFS_ITYPE_LNK:
498 return DT_LNK;
499 case UBIFS_ITYPE_BLK:
500 return DT_BLK;
501 case UBIFS_ITYPE_CHR:
502 return DT_CHR;
503 case UBIFS_ITYPE_FIFO:
504 return DT_FIFO;
505 case UBIFS_ITYPE_SOCK:
506 return DT_SOCK;
507 default:
508 BUG();
509 }
510 return 0;
511}
512
513/*
514 * The classical Unix view for directory is that it is a linear array of
515 * (name, inode number) entries. Linux/VFS assumes this model as well.
516 * Particularly, 'readdir()' call wants us to return a directory entry offset
517 * which later may be used to continue 'readdir()'ing the directory or to
518 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
519 * model because directory entries are identified by keys, which may collide.
520 *
521 * UBIFS uses directory entry hash value for directory offsets, so
522 * 'seekdir()'/'telldir()' may not always work because of possible key
523 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
524 * properly by means of saving full directory entry name in the private field
525 * of the file description object.
526 *
527 * This means that UBIFS cannot support NFS which requires full
528 * 'seekdir()'/'telldir()' support.
529 */
Al Viro01122e02013-05-16 01:14:46 -0400530static int ubifs_readdir(struct file *file, struct dir_context *ctx)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300531{
Richard Weinbergerba75d572016-12-14 11:09:25 +0100532 int fstr_real_len = 0, err = 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100533 struct fscrypt_name nm;
534 struct fscrypt_str fstr = {0};
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300535 union ubifs_key key;
536 struct ubifs_dent_node *dent;
Al Viro496ad9a2013-01-23 17:07:38 -0500537 struct inode *dir = file_inode(file);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300538 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100539 bool encrypted = ubifs_crypt_is_encrypted(dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300540
Al Viro01122e02013-05-16 01:14:46 -0400541 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300542
Al Viro01122e02013-05-16 01:14:46 -0400543 if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300544 /*
545 * The directory was seek'ed to a senseless position or there
546 * are no more entries.
547 */
548 return 0;
549
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100550 if (encrypted) {
551 err = fscrypt_get_encryption_info(dir);
552 if (err && err != -ENOKEY)
553 return err;
554
555 err = fscrypt_fname_alloc_buffer(dir, UBIFS_MAX_NLEN, &fstr);
556 if (err)
557 return err;
558
559 fstr_real_len = fstr.len;
560 }
561
Artem Bityutskiy605c9122013-06-28 14:15:15 +0300562 if (file->f_version == 0) {
563 /*
564 * The file was seek'ed, which means that @file->private_data
565 * is now invalid. This may also be just the first
566 * 'ubifs_readdir()' invocation, in which case
567 * @file->private_data is NULL, and the below code is
568 * basically a no-op.
569 */
570 kfree(file->private_data);
571 file->private_data = NULL;
572 }
573
574 /*
575 * 'generic_file_llseek()' unconditionally sets @file->f_version to
576 * zero, and we use this for detecting whether the file was seek'ed.
577 */
578 file->f_version = 1;
579
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300580 /* File positions 0 and 1 correspond to "." and ".." */
Al Viro01122e02013-05-16 01:14:46 -0400581 if (ctx->pos < 2) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300582 ubifs_assert(!file->private_data);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100583 if (!dir_emit_dots(file, ctx)) {
584 if (encrypted)
585 fscrypt_fname_free_buffer(&fstr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300586 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100587 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300588
589 /* Find the first entry in TNC and save it */
590 lowest_dent_key(c, &key, dir->i_ino);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100591 fname_len(&nm) = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300592 dent = ubifs_tnc_next_ent(c, &key, &nm);
593 if (IS_ERR(dent)) {
594 err = PTR_ERR(dent);
595 goto out;
596 }
597
Al Viro01122e02013-05-16 01:14:46 -0400598 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300599 file->private_data = dent;
600 }
601
602 dent = file->private_data;
603 if (!dent) {
604 /*
605 * The directory was seek'ed to and is now readdir'ed.
Al Viro01122e02013-05-16 01:14:46 -0400606 * Find the entry corresponding to @ctx->pos or the closest one.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300607 */
Al Viro01122e02013-05-16 01:14:46 -0400608 dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100609 fname_len(&nm) = 0;
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 }
Al Viro01122e02013-05-16 01:14:46 -0400615 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300616 file->private_data = dent;
617 }
618
619 while (1) {
620 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
Alexander Beregalov7424bac2008-09-17 22:09:41 +0400621 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300622 key_hash_flash(c, &dent->key));
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700623 ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
624 ubifs_inode(dir)->creat_sqnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300625
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100626 fname_len(&nm) = le16_to_cpu(dent->nlen);
627 fname_name(&nm) = dent->name;
628
629 if (encrypted) {
630 fstr.len = fstr_real_len;
631
Richard Weinberger528e3d12016-11-11 20:46:06 +0100632 err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
633 &dent->key),
634 le32_to_cpu(dent->cookie),
635 &nm.disk_name, &fstr);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +0200636 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100637 goto out;
638 } else {
639 fstr.len = fname_len(&nm);
640 fstr.name = fname_name(&nm);
641 }
642
643 if (!dir_emit(ctx, fstr.name, fstr.len,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300644 le64_to_cpu(dent->inum),
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100645 vfs_dent_type(dent->type))) {
646 if (encrypted)
647 fscrypt_fname_free_buffer(&fstr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300648 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100649 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300650
651 /* Switch to the next entry */
652 key_read(c, &dent->key, &key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300653 dent = ubifs_tnc_next_ent(c, &key, &nm);
654 if (IS_ERR(dent)) {
655 err = PTR_ERR(dent);
656 goto out;
657 }
658
659 kfree(file->private_data);
Al Viro01122e02013-05-16 01:14:46 -0400660 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300661 file->private_data = dent;
662 cond_resched();
663 }
664
665out:
Richard Weinbergeraeeb14f2015-10-12 23:35:36 +0200666 kfree(file->private_data);
667 file->private_data = NULL;
668
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100669 if (encrypted)
670 fscrypt_fname_free_buffer(&fstr);
671
Richard Weinbergerc83ed4c2016-10-19 12:43:07 +0200672 if (err != -ENOENT)
Sheng Yong235c3622015-03-20 10:39:42 +0000673 ubifs_err(c, "cannot find next direntry, error %d", err);
Richard Weinbergera00052a2016-10-28 11:49:03 +0200674 else
675 /*
676 * -ENOENT is a non-fatal error in this context, the TNC uses
677 * it to indicate that the cursor moved past the current directory
678 * and readdir() has to stop.
679 */
680 err = 0;
681
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300682
Artem Bityutskiy605c9122013-06-28 14:15:15 +0300683 /* 2 is a special value indicating that there are no more direntries */
Al Viro01122e02013-05-16 01:14:46 -0400684 ctx->pos = 2;
Richard Weinbergerc83ed4c2016-10-19 12:43:07 +0200685 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300686}
687
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300688/* Free saved readdir() state when the directory is closed */
689static int ubifs_dir_release(struct inode *dir, struct file *file)
690{
691 kfree(file->private_data);
692 file->private_data = NULL;
693 return 0;
694}
695
696/**
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200697 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300698 * @inode1: first inode
699 * @inode2: second inode
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200700 *
701 * We do not implement any tricks to guarantee strict lock ordering, because
702 * VFS has already done it for us on the @i_mutex. So this is just a simple
703 * wrapper function.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300704 */
705static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
706{
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200707 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
708 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300709}
710
711/**
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200712 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300713 * @inode1: first inode
714 * @inode2: second inode
715 */
716static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
717{
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300718 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200719 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300720}
721
722static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
723 struct dentry *dentry)
724{
725 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000726 struct inode *inode = d_inode(old_dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300727 struct ubifs_inode *ui = ubifs_inode(inode);
728 struct ubifs_inode *dir_ui = ubifs_inode(dir);
729 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
730 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +0300731 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100732 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300733
734 /*
735 * Budget request settings: new direntry, changing the target inode,
736 * changing the parent inode.
737 */
738
Al Viro4cb2a012013-09-16 10:58:53 -0400739 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
740 dentry, inode->i_ino,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300741 inode->i_nlink, dir->i_ino);
Al Viro59551022016-01-22 15:40:57 -0500742 ubifs_assert(inode_is_locked(dir));
743 ubifs_assert(inode_is_locked(inode));
Hunter Adrian8b3884a2009-05-14 06:32:30 +0200744
Eric Biggers3d4b2fc2016-12-19 11:15:48 -0800745 if (ubifs_crypt_is_encrypted(dir) &&
746 !fscrypt_has_permitted_context(dir, inode))
747 return -EPERM;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100748
749 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
750 if (err)
751 return err;
Richard Weinbergerac7e47a2016-09-29 20:00:38 +0200752
Artem Bityutskiyd808efb2011-05-31 18:14:38 +0300753 err = dbg_check_synced_i_size(c, inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300754 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100755 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300756
757 err = ubifs_budget_space(c, &req);
758 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100759 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300760
761 lock_2_inodes(dir, inode);
762 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400763 ihold(inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300764 inode->i_ctime = ubifs_current_time(inode);
765 dir->i_size += sz_change;
766 dir_ui->ui_size = dir->i_size;
767 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100768 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300769 if (err)
770 goto out_cancel;
771 unlock_2_inodes(dir, inode);
772
773 ubifs_release_budget(c, &req);
774 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100775 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300776 return 0;
777
778out_cancel:
779 dir->i_size -= sz_change;
780 dir_ui->ui_size = dir->i_size;
781 drop_nlink(inode);
782 unlock_2_inodes(dir, inode);
783 ubifs_release_budget(c, &req);
784 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100785out_fname:
786 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300787 return err;
788}
789
790static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
791{
792 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000793 struct inode *inode = d_inode(dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300794 struct ubifs_inode *dir_ui = ubifs_inode(dir);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100795 int err, sz_change, budgeted = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300796 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200797 unsigned int saved_nlink = inode->i_nlink;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100798 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300799
800 /*
801 * Budget request settings: deletion direntry, deletion inode (+1 for
802 * @dirtied_ino), changing the parent directory inode. If budgeting
803 * fails, go ahead anyway because we have extra space reserved for
804 * deletions.
805 */
806
Al Viro4cb2a012013-09-16 10:58:53 -0400807 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
808 dentry, inode->i_ino,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300809 inode->i_nlink, dir->i_ino);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100810
811 if (ubifs_crypt_is_encrypted(dir)) {
812 err = fscrypt_get_encryption_info(dir);
813 if (err && err != -ENOKEY)
814 return err;
815 }
816
817 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
818 if (err)
819 return err;
820
821 sz_change = CALC_DENT_SIZE(fname_len(&nm));
822
Al Viro59551022016-01-22 15:40:57 -0500823 ubifs_assert(inode_is_locked(dir));
824 ubifs_assert(inode_is_locked(inode));
Artem Bityutskiyd808efb2011-05-31 18:14:38 +0300825 err = dbg_check_synced_i_size(c, inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300826 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100827 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300828
829 err = ubifs_budget_space(c, &req);
830 if (err) {
831 if (err != -ENOSPC)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100832 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300833 budgeted = 0;
834 }
835
836 lock_2_inodes(dir, inode);
837 inode->i_ctime = ubifs_current_time(dir);
838 drop_nlink(inode);
839 dir->i_size -= sz_change;
840 dir_ui->ui_size = dir->i_size;
841 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100842 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300843 if (err)
844 goto out_cancel;
845 unlock_2_inodes(dir, inode);
846
847 if (budgeted)
848 ubifs_release_budget(c, &req);
849 else {
850 /* We've deleted something - clean the "no space" flags */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300851 c->bi.nospace = c->bi.nospace_rp = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300852 smp_wmb();
853 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100854 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300855 return 0;
856
857out_cancel:
858 dir->i_size += sz_change;
859 dir_ui->ui_size = dir->i_size;
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200860 set_nlink(inode, saved_nlink);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300861 unlock_2_inodes(dir, inode);
862 if (budgeted)
863 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100864out_fname:
865 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300866 return err;
867}
868
869/**
870 * check_dir_empty - check if a directory is empty or not.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300871 * @dir: VFS inode object of the directory to check
872 *
873 * This function checks if directory @dir is empty. Returns zero if the
874 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
875 * in case of of errors.
876 */
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200877int ubifs_check_dir_empty(struct inode *dir)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300878{
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200879 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100880 struct fscrypt_name nm = { 0 };
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300881 struct ubifs_dent_node *dent;
882 union ubifs_key key;
883 int err;
884
885 lowest_dent_key(c, &key, dir->i_ino);
886 dent = ubifs_tnc_next_ent(c, &key, &nm);
887 if (IS_ERR(dent)) {
888 err = PTR_ERR(dent);
889 if (err == -ENOENT)
890 err = 0;
891 } else {
892 kfree(dent);
893 err = -ENOTEMPTY;
894 }
895 return err;
896}
897
898static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
899{
900 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000901 struct inode *inode = d_inode(dentry);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100902 int err, sz_change, budgeted = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300903 struct ubifs_inode *dir_ui = ubifs_inode(dir);
904 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100905 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300906
907 /*
908 * Budget request settings: deletion direntry, deletion inode and
909 * changing the parent inode. If budgeting fails, go ahead anyway
910 * because we have extra space reserved for deletions.
911 */
912
Al Viro4cb2a012013-09-16 10:58:53 -0400913 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
914 inode->i_ino, dir->i_ino);
Al Viro59551022016-01-22 15:40:57 -0500915 ubifs_assert(inode_is_locked(dir));
916 ubifs_assert(inode_is_locked(inode));
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200917 err = ubifs_check_dir_empty(d_inode(dentry));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300918 if (err)
919 return err;
920
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100921 if (ubifs_crypt_is_encrypted(dir)) {
922 err = fscrypt_get_encryption_info(dir);
923 if (err && err != -ENOKEY)
924 return err;
925 }
926
927 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
928 if (err)
929 return err;
930
931 sz_change = CALC_DENT_SIZE(fname_len(&nm));
932
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300933 err = ubifs_budget_space(c, &req);
934 if (err) {
935 if (err != -ENOSPC)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100936 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300937 budgeted = 0;
938 }
939
940 lock_2_inodes(dir, inode);
941 inode->i_ctime = ubifs_current_time(dir);
942 clear_nlink(inode);
943 drop_nlink(dir);
944 dir->i_size -= sz_change;
945 dir_ui->ui_size = dir->i_size;
946 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100947 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300948 if (err)
949 goto out_cancel;
950 unlock_2_inodes(dir, inode);
951
952 if (budgeted)
953 ubifs_release_budget(c, &req);
954 else {
955 /* We've deleted something - clean the "no space" flags */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300956 c->bi.nospace = c->bi.nospace_rp = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300957 smp_wmb();
958 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100959 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300960 return 0;
961
962out_cancel:
963 dir->i_size += sz_change;
964 dir_ui->ui_size = dir->i_size;
965 inc_nlink(dir);
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200966 set_nlink(inode, 2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300967 unlock_2_inodes(dir, inode);
968 if (budgeted)
969 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100970out_fname:
971 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300972 return err;
973}
974
Al Viro18bb1db2011-07-26 01:41:39 -0400975static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300976{
977 struct inode *inode;
978 struct ubifs_inode *dir_ui = ubifs_inode(dir);
979 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100980 int err, sz_change;
Artem Bityutskiy182854b2008-07-18 18:54:29 +0300981 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100982 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300983
984 /*
985 * Budget request settings: new inode, new direntry and changing parent
986 * directory inode.
987 */
988
Al Viro4cb2a012013-09-16 10:58:53 -0400989 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
990 dentry, mode, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300991
992 err = ubifs_budget_space(c, &req);
993 if (err)
994 return err;
995
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100996 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
997 if (err)
998 goto out_budg;
999
1000 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1001
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001002 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
1003 if (IS_ERR(inode)) {
1004 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001005 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001006 }
1007
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001008 err = ubifs_init_security(dir, inode, &dentry->d_name);
1009 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001010 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001011
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001012 mutex_lock(&dir_ui->ui_mutex);
1013 insert_inode_hash(inode);
1014 inc_nlink(inode);
1015 inc_nlink(dir);
1016 dir->i_size += sz_change;
1017 dir_ui->ui_size = dir->i_size;
1018 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001019 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001020 if (err) {
Sheng Yong235c3622015-03-20 10:39:42 +00001021 ubifs_err(c, "cannot create directory, error %d", err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001022 goto out_cancel;
1023 }
1024 mutex_unlock(&dir_ui->ui_mutex);
1025
1026 ubifs_release_budget(c, &req);
1027 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001028 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001029 return 0;
1030
1031out_cancel:
1032 dir->i_size -= sz_change;
1033 dir_ui->ui_size = dir->i_size;
1034 drop_nlink(dir);
1035 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +01001036out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001037 make_bad_inode(inode);
1038 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001039out_fname:
1040 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001041out_budg:
1042 ubifs_release_budget(c, &req);
1043 return err;
1044}
1045
1046static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001047 umode_t mode, dev_t rdev)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001048{
1049 struct inode *inode;
1050 struct ubifs_inode *ui;
1051 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1052 struct ubifs_info *c = dir->i_sb->s_fs_info;
1053 union ubifs_dev_desc *dev = NULL;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001054 int sz_change;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001055 int err, devlen = 0;
1056 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001057 .new_ino_d = ALIGN(devlen, 8),
1058 .dirtied_ino = 1 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001059 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001060
1061 /*
1062 * Budget request settings: new inode, new direntry and changing parent
1063 * directory inode.
1064 */
1065
Al Viro4cb2a012013-09-16 10:58:53 -04001066 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001067
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001068 if (S_ISBLK(mode) || S_ISCHR(mode)) {
1069 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1070 if (!dev)
1071 return -ENOMEM;
1072 devlen = ubifs_encode_dev(dev, rdev);
1073 }
1074
1075 err = ubifs_budget_space(c, &req);
1076 if (err) {
1077 kfree(dev);
1078 return err;
1079 }
1080
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001081 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1082 if (err)
1083 goto out_budg;
1084
1085 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1086
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001087 inode = ubifs_new_inode(c, dir, mode);
1088 if (IS_ERR(inode)) {
1089 kfree(dev);
1090 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001091 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001092 }
1093
1094 init_special_inode(inode, inode->i_mode, rdev);
1095 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
1096 ui = ubifs_inode(inode);
1097 ui->data = dev;
1098 ui->data_len = devlen;
1099
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001100 err = ubifs_init_security(dir, inode, &dentry->d_name);
1101 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001102 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001103
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001104 mutex_lock(&dir_ui->ui_mutex);
1105 dir->i_size += sz_change;
1106 dir_ui->ui_size = dir->i_size;
1107 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001108 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001109 if (err)
1110 goto out_cancel;
1111 mutex_unlock(&dir_ui->ui_mutex);
1112
1113 ubifs_release_budget(c, &req);
1114 insert_inode_hash(inode);
1115 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001116 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001117 return 0;
1118
1119out_cancel:
1120 dir->i_size -= sz_change;
1121 dir_ui->ui_size = dir->i_size;
1122 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +01001123out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001124 make_bad_inode(inode);
1125 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001126out_fname:
1127 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001128out_budg:
1129 ubifs_release_budget(c, &req);
1130 return err;
1131}
1132
1133static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
1134 const char *symname)
1135{
1136 struct inode *inode;
1137 struct ubifs_inode *ui;
1138 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1139 struct ubifs_info *c = dir->i_sb->s_fs_info;
1140 int err, len = strlen(symname);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001141 int sz_change = CALC_DENT_SIZE(len);
1142 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
1143 struct fscrypt_symlink_data *sd = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001144 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001145 .new_ino_d = ALIGN(len, 8),
1146 .dirtied_ino = 1 };
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001147 struct fscrypt_name nm;
1148
1149 if (ubifs_crypt_is_encrypted(dir)) {
1150 err = fscrypt_get_encryption_info(dir);
1151 if (err)
1152 goto out_budg;
1153
1154 if (!fscrypt_has_encryption_key(dir)) {
1155 err = -EPERM;
1156 goto out_budg;
1157 }
1158
1159 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
1160 sizeof(struct fscrypt_symlink_data));
1161 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001162
1163 /*
1164 * Budget request settings: new inode, new direntry and changing parent
1165 * directory inode.
1166 */
1167
Al Viro4cb2a012013-09-16 10:58:53 -04001168 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
1169 symname, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001170
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001171 if (disk_link.len > UBIFS_MAX_INO_DATA)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001172 return -ENAMETOOLONG;
1173
1174 err = ubifs_budget_space(c, &req);
1175 if (err)
1176 return err;
1177
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001178 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1179 if (err)
1180 goto out_budg;
1181
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001182 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
1183 if (IS_ERR(inode)) {
1184 err = PTR_ERR(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001185 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001186 }
1187
1188 ui = ubifs_inode(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001189 ui->data = kmalloc(disk_link.len, GFP_NOFS);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001190 if (!ui->data) {
1191 err = -ENOMEM;
1192 goto out_inode;
1193 }
1194
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001195 if (ubifs_crypt_is_encrypted(dir)) {
1196 struct qstr istr = QSTR_INIT(symname, len);
1197 struct fscrypt_str ostr;
1198
1199 sd = kzalloc(disk_link.len, GFP_NOFS);
1200 if (!sd) {
1201 err = -ENOMEM;
1202 goto out_inode;
1203 }
1204
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001205 ostr.name = sd->encrypted_path;
1206 ostr.len = disk_link.len;
1207
1208 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
1209 if (err) {
1210 kfree(sd);
1211 goto out_inode;
1212 }
1213
1214 sd->len = cpu_to_le16(ostr.len);
1215 disk_link.name = (char *)sd;
1216 } else {
1217 inode->i_link = ui->data;
1218 }
1219
1220 memcpy(ui->data, disk_link.name, disk_link.len);
1221 ((char *)ui->data)[disk_link.len - 1] = '\0';
1222
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001223 /*
1224 * The terminating zero byte is not written to the flash media and it
1225 * is put just to make later in-memory string processing simpler. Thus,
1226 * data length is @len, not @len + %1.
1227 */
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001228 ui->data_len = disk_link.len - 1;
1229 inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001230
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001231 err = ubifs_init_security(dir, inode, &dentry->d_name);
1232 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001233 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001234
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001235 mutex_lock(&dir_ui->ui_mutex);
1236 dir->i_size += sz_change;
1237 dir_ui->ui_size = dir->i_size;
1238 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001239 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001240 if (err)
1241 goto out_cancel;
1242 mutex_unlock(&dir_ui->ui_mutex);
1243
1244 ubifs_release_budget(c, &req);
1245 insert_inode_hash(inode);
1246 d_instantiate(dentry, inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001247 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001248 return 0;
1249
1250out_cancel:
1251 dir->i_size -= sz_change;
1252 dir_ui->ui_size = dir->i_size;
1253 mutex_unlock(&dir_ui->ui_mutex);
1254out_inode:
1255 make_bad_inode(inode);
1256 iput(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001257out_fname:
1258 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001259out_budg:
1260 ubifs_release_budget(c, &req);
1261 return err;
1262}
1263
1264/**
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001265 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001266 * @inode1: first inode
1267 * @inode2: second inode
1268 * @inode3: third inode
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001269 * @inode4: fouth inode
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001270 *
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001271 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001272 * @inode2 whereas @inode3 and @inode4 may be %NULL.
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001273 *
1274 * We do not implement any tricks to guarantee strict lock ordering, because
1275 * VFS has already done it for us on the @i_mutex. So this is just a simple
1276 * wrapper function.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001277 */
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001278static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1279 struct inode *inode3, struct inode *inode4)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001280{
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001281 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1282 if (inode2 != inode1)
1283 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1284 if (inode3)
1285 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001286 if (inode4)
1287 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001288}
1289
1290/**
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001291 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001292 * @inode1: first inode
1293 * @inode2: second inode
1294 * @inode3: third inode
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001295 * @inode4: fouth inode
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001296 */
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001297static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1298 struct inode *inode3, struct inode *inode4)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001299{
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001300 if (inode4)
1301 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001302 if (inode3)
1303 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001304 if (inode1 != inode2)
1305 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1306 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001307}
1308
Richard Weinberger390975a2016-10-18 21:21:43 +02001309static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1310 struct inode *new_dir, struct dentry *new_dentry,
1311 unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001312{
1313 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +00001314 struct inode *old_inode = d_inode(old_dentry);
1315 struct inode *new_inode = d_inode(new_dentry);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001316 struct inode *whiteout = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001317 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001318 struct ubifs_inode *whiteout_ui = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001319 int err, release, sync = 0, move = (new_dir != old_dir);
1320 int is_dir = S_ISDIR(old_inode->i_mode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001321 int unlink = !!new_inode, new_sz, old_sz;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001322 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1323 .dirtied_ino = 3 };
1324 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001325 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001326 struct timespec time;
Alexandre Pereira da Silva782759b2012-06-25 17:47:49 -03001327 unsigned int uninitialized_var(saved_nlink);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001328 struct fscrypt_name old_nm, new_nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001329
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001330 if (flags & ~RENAME_NOREPLACE)
1331 return -EINVAL;
1332
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001333 /*
1334 * Budget request settings: deletion direntry, new direntry, removing
1335 * the old inode, and changing old and new parent directory inodes.
1336 *
1337 * However, this operation also marks the target inode as dirty and
1338 * does not write it, so we allocate budget for the target inode
1339 * separately.
1340 */
1341
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001342 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 -04001343 old_dentry, old_inode->i_ino, old_dir->i_ino,
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001344 new_dentry, new_dir->i_ino, flags);
1345
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001346 if (unlink)
Al Viro59551022016-01-22 15:40:57 -05001347 ubifs_assert(inode_is_locked(new_inode));
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001348
Richard Weinbergerac7e47a2016-09-29 20:00:38 +02001349 if (old_dir != new_dir) {
1350 if (ubifs_crypt_is_encrypted(new_dir) &&
1351 !fscrypt_has_permitted_context(new_dir, old_inode))
1352 return -EPERM;
1353 }
1354
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001355 if (unlink && is_dir) {
Richard Weinbergerf6337d82016-09-19 20:54:19 +02001356 err = ubifs_check_dir_empty(new_inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001357 if (err)
1358 return err;
1359 }
1360
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001361 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001362 if (err)
1363 return err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001364
1365 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
1366 if (err) {
1367 fscrypt_free_filename(&old_nm);
1368 return err;
1369 }
1370
1371 new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
1372 old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
1373
1374 err = ubifs_budget_space(c, &req);
1375 if (err) {
1376 fscrypt_free_filename(&old_nm);
1377 fscrypt_free_filename(&new_nm);
1378 return err;
1379 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001380 err = ubifs_budget_space(c, &ino_req);
1381 if (err) {
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001382 fscrypt_free_filename(&old_nm);
1383 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001384 ubifs_release_budget(c, &req);
1385 return err;
1386 }
1387
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001388 if (flags & RENAME_WHITEOUT) {
1389 union ubifs_dev_desc *dev = NULL;
1390
1391 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1392 if (!dev) {
1393 ubifs_release_budget(c, &req);
1394 ubifs_release_budget(c, &ino_req);
1395 return -ENOMEM;
1396 }
1397
1398 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1399 if (err) {
1400 ubifs_release_budget(c, &req);
1401 ubifs_release_budget(c, &ino_req);
1402 kfree(dev);
1403 return err;
1404 }
1405
1406 whiteout->i_state |= I_LINKABLE;
1407 whiteout_ui = ubifs_inode(whiteout);
1408 whiteout_ui->data = dev;
1409 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
1410 ubifs_assert(!whiteout_ui->dirty);
1411 }
1412
1413 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001414
1415 /*
1416 * Like most other Unix systems, set the @i_ctime for inodes on a
1417 * rename.
1418 */
1419 time = ubifs_current_time(old_dir);
1420 old_inode->i_ctime = time;
1421
1422 /* We must adjust parent link count when renaming directories */
1423 if (is_dir) {
1424 if (move) {
1425 /*
1426 * @old_dir loses a link because we are moving
1427 * @old_inode to a different directory.
1428 */
1429 drop_nlink(old_dir);
1430 /*
1431 * @new_dir only gains a link if we are not also
1432 * overwriting an existing directory.
1433 */
1434 if (!unlink)
1435 inc_nlink(new_dir);
1436 } else {
1437 /*
1438 * @old_inode is not moving to a different directory,
1439 * but @old_dir still loses a link if we are
1440 * overwriting an existing directory.
1441 */
1442 if (unlink)
1443 drop_nlink(old_dir);
1444 }
1445 }
1446
1447 old_dir->i_size -= old_sz;
1448 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1449 old_dir->i_mtime = old_dir->i_ctime = time;
1450 new_dir->i_mtime = new_dir->i_ctime = time;
1451
1452 /*
1453 * And finally, if we unlinked a direntry which happened to have the
1454 * same name as the moved direntry, we have to decrement @i_nlink of
1455 * the unlinked inode and change its ctime.
1456 */
1457 if (unlink) {
1458 /*
1459 * Directories cannot have hard-links, so if this is a
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001460 * directory, just clear @i_nlink.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001461 */
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001462 saved_nlink = new_inode->i_nlink;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001463 if (is_dir)
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001464 clear_nlink(new_inode);
1465 else
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001466 drop_nlink(new_inode);
1467 new_inode->i_ctime = time;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001468 } else {
1469 new_dir->i_size += new_sz;
1470 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1471 }
1472
1473 /*
1474 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1475 * is dirty, because this will be done later on at the end of
1476 * 'ubifs_rename()'.
1477 */
1478 if (IS_SYNC(old_inode)) {
1479 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1480 if (unlink && IS_SYNC(new_inode))
1481 sync = 1;
1482 }
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001483
1484 if (whiteout) {
1485 struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1486 .dirtied_ino_d = \
1487 ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1488
1489 err = ubifs_budget_space(c, &wht_req);
1490 if (err) {
1491 ubifs_release_budget(c, &req);
1492 ubifs_release_budget(c, &ino_req);
1493 kfree(whiteout_ui->data);
1494 whiteout_ui->data_len = 0;
1495 iput(whiteout);
1496 return err;
1497 }
1498
1499 inc_nlink(whiteout);
1500 mark_inode_dirty(whiteout);
1501 whiteout->i_state &= ~I_LINKABLE;
1502 iput(whiteout);
1503 }
1504
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001505 err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
1506 new_inode, &new_nm, whiteout, sync);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001507 if (err)
1508 goto out_cancel;
1509
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001510 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001511 ubifs_release_budget(c, &req);
1512
1513 mutex_lock(&old_inode_ui->ui_mutex);
1514 release = old_inode_ui->dirty;
1515 mark_inode_dirty_sync(old_inode);
1516 mutex_unlock(&old_inode_ui->ui_mutex);
1517
1518 if (release)
1519 ubifs_release_budget(c, &ino_req);
1520 if (IS_SYNC(old_inode))
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001521 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001522
1523 fscrypt_free_filename(&old_nm);
1524 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001525 return err;
1526
1527out_cancel:
1528 if (unlink) {
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001529 set_nlink(new_inode, saved_nlink);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001530 } else {
1531 new_dir->i_size -= new_sz;
1532 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1533 }
1534 old_dir->i_size += old_sz;
1535 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1536 if (is_dir) {
1537 if (move) {
1538 inc_nlink(old_dir);
1539 if (!unlink)
1540 drop_nlink(new_dir);
1541 } else {
1542 if (unlink)
1543 inc_nlink(old_dir);
1544 }
1545 }
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001546 if (whiteout) {
1547 drop_nlink(whiteout);
1548 iput(whiteout);
1549 }
1550 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001551 ubifs_release_budget(c, &ino_req);
1552 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001553 fscrypt_free_filename(&old_nm);
1554 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001555 return err;
1556}
1557
Richard Weinberger9ec64962016-09-14 22:28:51 +02001558static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1559 struct inode *new_dir, struct dentry *new_dentry)
1560{
1561 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1562 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1563 .dirtied_ino = 2 };
1564 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1565 struct inode *fst_inode = d_inode(old_dentry);
1566 struct inode *snd_inode = d_inode(new_dentry);
1567 struct timespec time;
1568 int err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001569 struct fscrypt_name fst_nm, snd_nm;
Richard Weinberger9ec64962016-09-14 22:28:51 +02001570
1571 ubifs_assert(fst_inode && snd_inode);
1572
Richard Weinbergerac7e47a2016-09-29 20:00:38 +02001573 if ((ubifs_crypt_is_encrypted(old_dir) ||
1574 ubifs_crypt_is_encrypted(new_dir)) &&
1575 (old_dir != new_dir) &&
1576 (!fscrypt_has_permitted_context(new_dir, fst_inode) ||
1577 !fscrypt_has_permitted_context(old_dir, snd_inode)))
1578 return -EPERM;
1579
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001580 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
1581 if (err)
1582 return err;
1583
1584 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
1585 if (err) {
1586 fscrypt_free_filename(&fst_nm);
1587 return err;
1588 }
1589
Richard Weinberger9ec64962016-09-14 22:28:51 +02001590 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1591
1592 time = ubifs_current_time(old_dir);
1593 fst_inode->i_ctime = time;
1594 snd_inode->i_ctime = time;
1595 old_dir->i_mtime = old_dir->i_ctime = time;
1596 new_dir->i_mtime = new_dir->i_ctime = time;
1597
1598 if (old_dir != new_dir) {
1599 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1600 inc_nlink(new_dir);
1601 drop_nlink(old_dir);
1602 }
1603 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1604 drop_nlink(new_dir);
1605 inc_nlink(old_dir);
1606 }
1607 }
1608
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001609 err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
1610 snd_inode, &snd_nm, sync);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001611
1612 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1613 ubifs_release_budget(c, &req);
1614
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001615 fscrypt_free_filename(&fst_nm);
1616 fscrypt_free_filename(&snd_nm);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001617 return err;
1618}
1619
Richard Weinberger390975a2016-10-18 21:21:43 +02001620static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
Richard Weinberger9ec64962016-09-14 22:28:51 +02001621 struct inode *new_dir, struct dentry *new_dentry,
1622 unsigned int flags)
1623{
1624 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1625 return -EINVAL;
1626
1627 ubifs_assert(inode_is_locked(old_dir));
1628 ubifs_assert(inode_is_locked(new_dir));
1629
1630 if (flags & RENAME_EXCHANGE)
1631 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1632
Richard Weinberger390975a2016-10-18 21:21:43 +02001633 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001634}
1635
David Howellsa528d352017-01-31 16:46:22 +00001636int ubifs_getattr(const struct path *path, struct kstat *stat,
1637 u32 request_mask, unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001638{
1639 loff_t size;
David Howellsa528d352017-01-31 16:46:22 +00001640 struct inode *inode = d_inode(path->dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001641 struct ubifs_inode *ui = ubifs_inode(inode);
1642
1643 mutex_lock(&ui->ui_mutex);
Al Viro6d42e7e2012-04-02 14:25:07 -04001644 generic_fillattr(inode, stat);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001645 stat->blksize = UBIFS_BLOCK_SIZE;
1646 stat->size = ui->ui_size;
1647
1648 /*
1649 * Unfortunately, the 'stat()' system call was designed for block
1650 * device based file systems, and it is not appropriate for UBIFS,
1651 * because UBIFS does not have notion of "block". For example, it is
1652 * difficult to tell how many block a directory takes - it actually
1653 * takes less than 300 bytes, but we have to round it to block size,
1654 * which introduces large mistake. This makes utilities like 'du' to
1655 * report completely senseless numbers. This is the reason why UBIFS
1656 * goes the same way as JFFS2 - it reports zero blocks for everything
1657 * but regular files, which makes more sense than reporting completely
1658 * wrong sizes.
1659 */
1660 if (S_ISREG(inode->i_mode)) {
1661 size = ui->xattr_size;
1662 size += stat->size;
1663 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1664 /*
1665 * Note, user-space expects 512-byte blocks count irrespectively
1666 * of what was reported in @stat->size.
1667 */
1668 stat->blocks = size >> 9;
1669 } else
1670 stat->blocks = 0;
1671 mutex_unlock(&ui->ui_mutex);
1672 return 0;
1673}
1674
Richard Weinbergerba40e6a2016-09-29 17:20:27 +02001675static int ubifs_dir_open(struct inode *dir, struct file *file)
1676{
1677 if (ubifs_crypt_is_encrypted(dir))
1678 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1679
1680 return 0;
1681}
1682
Artem Bityutskiye8b81562009-01-15 17:43:23 +02001683const struct inode_operations ubifs_dir_inode_operations = {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001684 .lookup = ubifs_lookup,
1685 .create = ubifs_create,
1686 .link = ubifs_link,
1687 .symlink = ubifs_symlink,
1688 .unlink = ubifs_unlink,
1689 .mkdir = ubifs_mkdir,
1690 .rmdir = ubifs_rmdir,
1691 .mknod = ubifs_mknod,
Richard Weinberger390975a2016-10-18 21:21:43 +02001692 .rename = ubifs_rename,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001693 .setattr = ubifs_setattr,
1694 .getattr = ubifs_getattr,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001695 .listxattr = ubifs_listxattr,
Dongsheng Yang8c1c5f22015-11-07 12:46:11 +08001696#ifdef CONFIG_UBIFS_ATIME_SUPPORT
1697 .update_time = ubifs_update_time,
1698#endif
Richard Weinberger474b9372016-09-14 22:28:49 +02001699 .tmpfile = ubifs_tmpfile,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001700};
1701
Artem Bityutskiye8b81562009-01-15 17:43:23 +02001702const struct file_operations ubifs_dir_operations = {
Al Viro01122e02013-05-16 01:14:46 -04001703 .llseek = generic_file_llseek,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001704 .release = ubifs_dir_release,
1705 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001706 .iterate_shared = ubifs_readdir,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001707 .fsync = ubifs_fsync,
1708 .unlocked_ioctl = ubifs_ioctl,
Richard Weinbergerba40e6a2016-09-29 17:20:27 +02001709 .open = ubifs_dir_open,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001710#ifdef CONFIG_COMPAT
1711 .compat_ioctl = ubifs_compat_ioctl,
1712#endif
1713};