blob: 1c5331ac9614040016019aca78c1338133ea4e11 [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
288done:
289 kfree(dent);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100290 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300291 /*
292 * Note, d_splice_alias() would be required instead if we supported
293 * NFS.
294 */
295 d_add(dentry, inode);
296 return NULL;
297
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100298out_dent:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300299 kfree(dent);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100300out_fname:
301 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300302 return ERR_PTR(err);
303}
304
Al Viro4acdaf22011-07-26 01:42:34 -0400305static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400306 bool excl)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300307{
308 struct inode *inode;
309 struct ubifs_info *c = dir->i_sb->s_fs_info;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300310 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
311 .dirtied_ino = 1 };
312 struct ubifs_inode *dir_ui = ubifs_inode(dir);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100313 struct fscrypt_name nm;
314 int err, sz_change;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300315
316 /*
317 * Budget request settings: new inode, new direntry, changing the
318 * parent directory inode.
319 */
320
Al Viro4cb2a012013-09-16 10:58:53 -0400321 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
322 dentry, mode, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300323
324 err = ubifs_budget_space(c, &req);
325 if (err)
326 return err;
327
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100328 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
329 if (err)
330 goto out_budg;
331
332 sz_change = CALC_DENT_SIZE(fname_len(&nm));
333
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300334 inode = ubifs_new_inode(c, dir, mode);
335 if (IS_ERR(inode)) {
336 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100337 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300338 }
339
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500340 err = ubifs_init_security(dir, inode, &dentry->d_name);
341 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +0100342 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -0500343
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300344 mutex_lock(&dir_ui->ui_mutex);
345 dir->i_size += sz_change;
346 dir_ui->ui_size = dir->i_size;
347 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100348 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300349 if (err)
350 goto out_cancel;
351 mutex_unlock(&dir_ui->ui_mutex);
352
353 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100354 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300355 insert_inode_hash(inode);
356 d_instantiate(dentry, inode);
357 return 0;
358
359out_cancel:
360 dir->i_size -= sz_change;
361 dir_ui->ui_size = dir->i_size;
362 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +0100363out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300364 make_bad_inode(inode);
365 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100366out_fname:
367 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300368out_budg:
369 ubifs_release_budget(c, &req);
Sheng Yong235c3622015-03-20 10:39:42 +0000370 ubifs_err(c, "cannot create regular file, error %d", err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300371 return err;
372}
373
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200374static int do_tmpfile(struct inode *dir, struct dentry *dentry,
375 umode_t mode, struct inode **whiteout)
Richard Weinberger474b9372016-09-14 22:28:49 +0200376{
377 struct inode *inode;
378 struct ubifs_info *c = dir->i_sb->s_fs_info;
379 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1};
380 struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
381 struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
382 int err, instantiated = 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100383 struct fscrypt_name nm;
Richard Weinberger474b9372016-09-14 22:28:49 +0200384
385 /*
386 * Budget request settings: new dirty inode, new direntry,
387 * budget for dirtied inode will be released via writeback.
388 */
389
390 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
391 dentry, mode, dir->i_ino);
392
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100393 if (ubifs_crypt_is_encrypted(dir)) {
394 err = fscrypt_get_encryption_info(dir);
395 if (err)
396 return err;
397
398 if (!fscrypt_has_encryption_key(dir)) {
399 return -EPERM;
400 }
401 }
402
403 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200404 if (err)
405 return err;
406
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100407 err = ubifs_budget_space(c, &req);
408 if (err) {
409 fscrypt_free_filename(&nm);
410 return err;
411 }
412
Richard Weinberger474b9372016-09-14 22:28:49 +0200413 err = ubifs_budget_space(c, &ino_req);
414 if (err) {
415 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100416 fscrypt_free_filename(&nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200417 return err;
418 }
419
420 inode = ubifs_new_inode(c, dir, mode);
421 if (IS_ERR(inode)) {
422 err = PTR_ERR(inode);
423 goto out_budg;
424 }
425 ui = ubifs_inode(inode);
426
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200427 if (whiteout) {
428 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
429 ubifs_assert(inode->i_op == &ubifs_file_inode_operations);
430 }
431
Richard Weinberger474b9372016-09-14 22:28:49 +0200432 err = ubifs_init_security(dir, inode, &dentry->d_name);
433 if (err)
434 goto out_inode;
435
436 mutex_lock(&ui->ui_mutex);
437 insert_inode_hash(inode);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200438
439 if (whiteout) {
440 mark_inode_dirty(inode);
441 drop_nlink(inode);
442 *whiteout = inode;
443 } else {
444 d_tmpfile(dentry, inode);
445 }
Richard Weinberger474b9372016-09-14 22:28:49 +0200446 ubifs_assert(ui->dirty);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200447
Richard Weinberger474b9372016-09-14 22:28:49 +0200448 instantiated = 1;
449 mutex_unlock(&ui->ui_mutex);
450
451 mutex_lock(&dir_ui->ui_mutex);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100452 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Richard Weinberger474b9372016-09-14 22:28:49 +0200453 if (err)
454 goto out_cancel;
455 mutex_unlock(&dir_ui->ui_mutex);
456
457 ubifs_release_budget(c, &req);
458
459 return 0;
460
461out_cancel:
462 mutex_unlock(&dir_ui->ui_mutex);
463out_inode:
464 make_bad_inode(inode);
465 if (!instantiated)
466 iput(inode);
467out_budg:
468 ubifs_release_budget(c, &req);
469 if (!instantiated)
470 ubifs_release_budget(c, &ino_req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100471 fscrypt_free_filename(&nm);
Richard Weinberger474b9372016-09-14 22:28:49 +0200472 ubifs_err(c, "cannot create temporary file, error %d", err);
473 return err;
474}
475
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +0200476static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
477 umode_t mode)
478{
479 return do_tmpfile(dir, dentry, mode, NULL);
480}
481
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300482/**
483 * vfs_dent_type - get VFS directory entry type.
484 * @type: UBIFS directory entry type
485 *
486 * This function converts UBIFS directory entry type into VFS directory entry
487 * type.
488 */
489static unsigned int vfs_dent_type(uint8_t type)
490{
491 switch (type) {
492 case UBIFS_ITYPE_REG:
493 return DT_REG;
494 case UBIFS_ITYPE_DIR:
495 return DT_DIR;
496 case UBIFS_ITYPE_LNK:
497 return DT_LNK;
498 case UBIFS_ITYPE_BLK:
499 return DT_BLK;
500 case UBIFS_ITYPE_CHR:
501 return DT_CHR;
502 case UBIFS_ITYPE_FIFO:
503 return DT_FIFO;
504 case UBIFS_ITYPE_SOCK:
505 return DT_SOCK;
506 default:
507 BUG();
508 }
509 return 0;
510}
511
512/*
513 * The classical Unix view for directory is that it is a linear array of
514 * (name, inode number) entries. Linux/VFS assumes this model as well.
515 * Particularly, 'readdir()' call wants us to return a directory entry offset
516 * which later may be used to continue 'readdir()'ing the directory or to
517 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
518 * model because directory entries are identified by keys, which may collide.
519 *
520 * UBIFS uses directory entry hash value for directory offsets, so
521 * 'seekdir()'/'telldir()' may not always work because of possible key
522 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
523 * properly by means of saving full directory entry name in the private field
524 * of the file description object.
525 *
526 * This means that UBIFS cannot support NFS which requires full
527 * 'seekdir()'/'telldir()' support.
528 */
Al Viro01122e02013-05-16 01:14:46 -0400529static int ubifs_readdir(struct file *file, struct dir_context *ctx)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300530{
Richard Weinbergerba75d572016-12-14 11:09:25 +0100531 int fstr_real_len = 0, err = 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100532 struct fscrypt_name nm;
533 struct fscrypt_str fstr = {0};
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300534 union ubifs_key key;
535 struct ubifs_dent_node *dent;
Al Viro496ad9a2013-01-23 17:07:38 -0500536 struct inode *dir = file_inode(file);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300537 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100538 bool encrypted = ubifs_crypt_is_encrypted(dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300539
Al Viro01122e02013-05-16 01:14:46 -0400540 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300541
Al Viro01122e02013-05-16 01:14:46 -0400542 if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300543 /*
544 * The directory was seek'ed to a senseless position or there
545 * are no more entries.
546 */
547 return 0;
548
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100549 if (encrypted) {
550 err = fscrypt_get_encryption_info(dir);
551 if (err && err != -ENOKEY)
552 return err;
553
554 err = fscrypt_fname_alloc_buffer(dir, UBIFS_MAX_NLEN, &fstr);
555 if (err)
556 return err;
557
558 fstr_real_len = fstr.len;
559 }
560
Artem Bityutskiy605c9122013-06-28 14:15:15 +0300561 if (file->f_version == 0) {
562 /*
563 * The file was seek'ed, which means that @file->private_data
564 * is now invalid. This may also be just the first
565 * 'ubifs_readdir()' invocation, in which case
566 * @file->private_data is NULL, and the below code is
567 * basically a no-op.
568 */
569 kfree(file->private_data);
570 file->private_data = NULL;
571 }
572
573 /*
574 * 'generic_file_llseek()' unconditionally sets @file->f_version to
575 * zero, and we use this for detecting whether the file was seek'ed.
576 */
577 file->f_version = 1;
578
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300579 /* File positions 0 and 1 correspond to "." and ".." */
Al Viro01122e02013-05-16 01:14:46 -0400580 if (ctx->pos < 2) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300581 ubifs_assert(!file->private_data);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100582 if (!dir_emit_dots(file, ctx)) {
583 if (encrypted)
584 fscrypt_fname_free_buffer(&fstr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300585 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100586 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300587
588 /* Find the first entry in TNC and save it */
589 lowest_dent_key(c, &key, dir->i_ino);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100590 fname_len(&nm) = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300591 dent = ubifs_tnc_next_ent(c, &key, &nm);
592 if (IS_ERR(dent)) {
593 err = PTR_ERR(dent);
594 goto out;
595 }
596
Al Viro01122e02013-05-16 01:14:46 -0400597 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300598 file->private_data = dent;
599 }
600
601 dent = file->private_data;
602 if (!dent) {
603 /*
604 * The directory was seek'ed to and is now readdir'ed.
Al Viro01122e02013-05-16 01:14:46 -0400605 * Find the entry corresponding to @ctx->pos or the closest one.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300606 */
Al Viro01122e02013-05-16 01:14:46 -0400607 dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100608 fname_len(&nm) = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300609 dent = ubifs_tnc_next_ent(c, &key, &nm);
610 if (IS_ERR(dent)) {
611 err = PTR_ERR(dent);
612 goto out;
613 }
Al Viro01122e02013-05-16 01:14:46 -0400614 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300615 file->private_data = dent;
616 }
617
618 while (1) {
619 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
Alexander Beregalov7424bac2008-09-17 22:09:41 +0400620 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300621 key_hash_flash(c, &dent->key));
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700622 ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
623 ubifs_inode(dir)->creat_sqnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300624
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100625 fname_len(&nm) = le16_to_cpu(dent->nlen);
626 fname_name(&nm) = dent->name;
627
628 if (encrypted) {
629 fstr.len = fstr_real_len;
630
Richard Weinberger528e3d12016-11-11 20:46:06 +0100631 err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
632 &dent->key),
633 le32_to_cpu(dent->cookie),
634 &nm.disk_name, &fstr);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +0200635 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100636 goto out;
637 } else {
638 fstr.len = fname_len(&nm);
639 fstr.name = fname_name(&nm);
640 }
641
642 if (!dir_emit(ctx, fstr.name, fstr.len,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300643 le64_to_cpu(dent->inum),
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100644 vfs_dent_type(dent->type))) {
645 if (encrypted)
646 fscrypt_fname_free_buffer(&fstr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300647 return 0;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100648 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300649
650 /* Switch to the next entry */
651 key_read(c, &dent->key, &key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300652 dent = ubifs_tnc_next_ent(c, &key, &nm);
653 if (IS_ERR(dent)) {
654 err = PTR_ERR(dent);
655 goto out;
656 }
657
658 kfree(file->private_data);
Al Viro01122e02013-05-16 01:14:46 -0400659 ctx->pos = key_hash_flash(c, &dent->key);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300660 file->private_data = dent;
661 cond_resched();
662 }
663
664out:
Richard Weinbergeraeeb14f2015-10-12 23:35:36 +0200665 kfree(file->private_data);
666 file->private_data = NULL;
667
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100668 if (encrypted)
669 fscrypt_fname_free_buffer(&fstr);
670
Richard Weinbergerc83ed4c2016-10-19 12:43:07 +0200671 if (err != -ENOENT)
Sheng Yong235c3622015-03-20 10:39:42 +0000672 ubifs_err(c, "cannot find next direntry, error %d", err);
Richard Weinbergera00052a2016-10-28 11:49:03 +0200673 else
674 /*
675 * -ENOENT is a non-fatal error in this context, the TNC uses
676 * it to indicate that the cursor moved past the current directory
677 * and readdir() has to stop.
678 */
679 err = 0;
680
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300681
Artem Bityutskiy605c9122013-06-28 14:15:15 +0300682 /* 2 is a special value indicating that there are no more direntries */
Al Viro01122e02013-05-16 01:14:46 -0400683 ctx->pos = 2;
Richard Weinbergerc83ed4c2016-10-19 12:43:07 +0200684 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300685}
686
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300687/* Free saved readdir() state when the directory is closed */
688static int ubifs_dir_release(struct inode *dir, struct file *file)
689{
690 kfree(file->private_data);
691 file->private_data = NULL;
692 return 0;
693}
694
695/**
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200696 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300697 * @inode1: first inode
698 * @inode2: second inode
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200699 *
700 * We do not implement any tricks to guarantee strict lock ordering, because
701 * VFS has already done it for us on the @i_mutex. So this is just a simple
702 * wrapper function.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300703 */
704static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
705{
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200706 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
707 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300708}
709
710/**
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200711 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300712 * @inode1: first inode
713 * @inode2: second inode
714 */
715static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
716{
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300717 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
Artem Bityutskiy82c15932009-01-20 16:46:02 +0200718 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300719}
720
721static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
722 struct dentry *dentry)
723{
724 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000725 struct inode *inode = d_inode(old_dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300726 struct ubifs_inode *ui = ubifs_inode(inode);
727 struct ubifs_inode *dir_ui = ubifs_inode(dir);
728 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
729 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +0300730 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100731 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300732
733 /*
734 * Budget request settings: new direntry, changing the target inode,
735 * changing the parent inode.
736 */
737
Al Viro4cb2a012013-09-16 10:58:53 -0400738 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
739 dentry, inode->i_ino,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300740 inode->i_nlink, dir->i_ino);
Al Viro59551022016-01-22 15:40:57 -0500741 ubifs_assert(inode_is_locked(dir));
742 ubifs_assert(inode_is_locked(inode));
Hunter Adrian8b3884a2009-05-14 06:32:30 +0200743
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100744 if (ubifs_crypt_is_encrypted(dir)) {
745 if (!fscrypt_has_permitted_context(dir, inode))
746 return -EPERM;
747
748 err = fscrypt_get_encryption_info(inode);
749 if (err)
750 return err;
751
752 if (!fscrypt_has_encryption_key(inode))
753 return -EPERM;
754 }
755
756 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
757 if (err)
758 return err;
Richard Weinbergerac7e47a2016-09-29 20:00:38 +0200759
Artem Bityutskiyd808efb2011-05-31 18:14:38 +0300760 err = dbg_check_synced_i_size(c, inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300761 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100762 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300763
764 err = ubifs_budget_space(c, &req);
765 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100766 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300767
768 lock_2_inodes(dir, inode);
769 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400770 ihold(inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300771 inode->i_ctime = ubifs_current_time(inode);
772 dir->i_size += sz_change;
773 dir_ui->ui_size = dir->i_size;
774 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100775 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300776 if (err)
777 goto out_cancel;
778 unlock_2_inodes(dir, inode);
779
780 ubifs_release_budget(c, &req);
781 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100782 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300783 return 0;
784
785out_cancel:
786 dir->i_size -= sz_change;
787 dir_ui->ui_size = dir->i_size;
788 drop_nlink(inode);
789 unlock_2_inodes(dir, inode);
790 ubifs_release_budget(c, &req);
791 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100792out_fname:
793 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300794 return err;
795}
796
797static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
798{
799 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000800 struct inode *inode = d_inode(dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300801 struct ubifs_inode *dir_ui = ubifs_inode(dir);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100802 int err, sz_change, budgeted = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300803 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200804 unsigned int saved_nlink = inode->i_nlink;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100805 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300806
807 /*
808 * Budget request settings: deletion direntry, deletion inode (+1 for
809 * @dirtied_ino), changing the parent directory inode. If budgeting
810 * fails, go ahead anyway because we have extra space reserved for
811 * deletions.
812 */
813
Al Viro4cb2a012013-09-16 10:58:53 -0400814 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
815 dentry, inode->i_ino,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300816 inode->i_nlink, dir->i_ino);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100817
818 if (ubifs_crypt_is_encrypted(dir)) {
819 err = fscrypt_get_encryption_info(dir);
820 if (err && err != -ENOKEY)
821 return err;
822 }
823
824 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
825 if (err)
826 return err;
827
828 sz_change = CALC_DENT_SIZE(fname_len(&nm));
829
Al Viro59551022016-01-22 15:40:57 -0500830 ubifs_assert(inode_is_locked(dir));
831 ubifs_assert(inode_is_locked(inode));
Artem Bityutskiyd808efb2011-05-31 18:14:38 +0300832 err = dbg_check_synced_i_size(c, inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300833 if (err)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100834 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300835
836 err = ubifs_budget_space(c, &req);
837 if (err) {
838 if (err != -ENOSPC)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100839 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300840 budgeted = 0;
841 }
842
843 lock_2_inodes(dir, inode);
844 inode->i_ctime = ubifs_current_time(dir);
845 drop_nlink(inode);
846 dir->i_size -= sz_change;
847 dir_ui->ui_size = dir->i_size;
848 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100849 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300850 if (err)
851 goto out_cancel;
852 unlock_2_inodes(dir, inode);
853
854 if (budgeted)
855 ubifs_release_budget(c, &req);
856 else {
857 /* We've deleted something - clean the "no space" flags */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300858 c->bi.nospace = c->bi.nospace_rp = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300859 smp_wmb();
860 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100861 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300862 return 0;
863
864out_cancel:
865 dir->i_size += sz_change;
866 dir_ui->ui_size = dir->i_size;
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200867 set_nlink(inode, saved_nlink);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300868 unlock_2_inodes(dir, inode);
869 if (budgeted)
870 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100871out_fname:
872 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300873 return err;
874}
875
876/**
877 * check_dir_empty - check if a directory is empty or not.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300878 * @dir: VFS inode object of the directory to check
879 *
880 * This function checks if directory @dir is empty. Returns zero if the
881 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
882 * in case of of errors.
883 */
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200884int ubifs_check_dir_empty(struct inode *dir)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300885{
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200886 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100887 struct fscrypt_name nm = { 0 };
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300888 struct ubifs_dent_node *dent;
889 union ubifs_key key;
890 int err;
891
892 lowest_dent_key(c, &key, dir->i_ino);
893 dent = ubifs_tnc_next_ent(c, &key, &nm);
894 if (IS_ERR(dent)) {
895 err = PTR_ERR(dent);
896 if (err == -ENOENT)
897 err = 0;
898 } else {
899 kfree(dent);
900 err = -ENOTEMPTY;
901 }
902 return err;
903}
904
905static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
906{
907 struct ubifs_info *c = dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000908 struct inode *inode = d_inode(dentry);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100909 int err, sz_change, budgeted = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300910 struct ubifs_inode *dir_ui = ubifs_inode(dir);
911 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100912 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300913
914 /*
915 * Budget request settings: deletion direntry, deletion inode and
916 * changing the parent inode. If budgeting fails, go ahead anyway
917 * because we have extra space reserved for deletions.
918 */
919
Al Viro4cb2a012013-09-16 10:58:53 -0400920 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
921 inode->i_ino, dir->i_ino);
Al Viro59551022016-01-22 15:40:57 -0500922 ubifs_assert(inode_is_locked(dir));
923 ubifs_assert(inode_is_locked(inode));
Richard Weinbergerf6337d82016-09-19 20:54:19 +0200924 err = ubifs_check_dir_empty(d_inode(dentry));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300925 if (err)
926 return err;
927
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100928 if (ubifs_crypt_is_encrypted(dir)) {
929 err = fscrypt_get_encryption_info(dir);
930 if (err && err != -ENOKEY)
931 return err;
932 }
933
934 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
935 if (err)
936 return err;
937
938 sz_change = CALC_DENT_SIZE(fname_len(&nm));
939
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300940 err = ubifs_budget_space(c, &req);
941 if (err) {
942 if (err != -ENOSPC)
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100943 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300944 budgeted = 0;
945 }
946
947 lock_2_inodes(dir, inode);
948 inode->i_ctime = ubifs_current_time(dir);
949 clear_nlink(inode);
950 drop_nlink(dir);
951 dir->i_size -= sz_change;
952 dir_ui->ui_size = dir->i_size;
953 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100954 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300955 if (err)
956 goto out_cancel;
957 unlock_2_inodes(dir, inode);
958
959 if (budgeted)
960 ubifs_release_budget(c, &req);
961 else {
962 /* We've deleted something - clean the "no space" flags */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300963 c->bi.nospace = c->bi.nospace_rp = 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300964 smp_wmb();
965 }
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100966 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300967 return 0;
968
969out_cancel:
970 dir->i_size += sz_change;
971 dir_ui->ui_size = dir->i_size;
972 inc_nlink(dir);
Artem Bityutskiyc43be102012-02-07 10:58:51 +0200973 set_nlink(inode, 2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300974 unlock_2_inodes(dir, inode);
975 if (budgeted)
976 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100977out_fname:
978 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300979 return err;
980}
981
Al Viro18bb1db2011-07-26 01:41:39 -0400982static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300983{
984 struct inode *inode;
985 struct ubifs_inode *dir_ui = ubifs_inode(dir);
986 struct ubifs_info *c = dir->i_sb->s_fs_info;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100987 int err, sz_change;
Artem Bityutskiy182854b2008-07-18 18:54:29 +0300988 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +0100989 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300990
991 /*
992 * Budget request settings: new inode, new direntry and changing parent
993 * directory inode.
994 */
995
Al Viro4cb2a012013-09-16 10:58:53 -0400996 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
997 dentry, mode, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300998
999 err = ubifs_budget_space(c, &req);
1000 if (err)
1001 return err;
1002
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001003 if (ubifs_crypt_is_encrypted(dir)) {
1004 err = fscrypt_get_encryption_info(dir);
1005 if (err)
1006 goto out_budg;
1007
1008 if (!fscrypt_has_encryption_key(dir)) {
1009 err = -EPERM;
1010 goto out_budg;
1011 }
1012 }
1013
1014 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1015 if (err)
1016 goto out_budg;
1017
1018 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1019
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001020 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
1021 if (IS_ERR(inode)) {
1022 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001023 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001024 }
1025
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001026 err = ubifs_init_security(dir, inode, &dentry->d_name);
1027 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001028 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001029
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001030 mutex_lock(&dir_ui->ui_mutex);
1031 insert_inode_hash(inode);
1032 inc_nlink(inode);
1033 inc_nlink(dir);
1034 dir->i_size += sz_change;
1035 dir_ui->ui_size = dir->i_size;
1036 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001037 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001038 if (err) {
Sheng Yong235c3622015-03-20 10:39:42 +00001039 ubifs_err(c, "cannot create directory, error %d", err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001040 goto out_cancel;
1041 }
1042 mutex_unlock(&dir_ui->ui_mutex);
1043
1044 ubifs_release_budget(c, &req);
1045 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001046 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001047 return 0;
1048
1049out_cancel:
1050 dir->i_size -= sz_change;
1051 dir_ui->ui_size = dir->i_size;
1052 drop_nlink(dir);
1053 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +01001054out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001055 make_bad_inode(inode);
1056 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001057out_fname:
1058 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001059out_budg:
1060 ubifs_release_budget(c, &req);
1061 return err;
1062}
1063
1064static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04001065 umode_t mode, dev_t rdev)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001066{
1067 struct inode *inode;
1068 struct ubifs_inode *ui;
1069 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1070 struct ubifs_info *c = dir->i_sb->s_fs_info;
1071 union ubifs_dev_desc *dev = NULL;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001072 int sz_change;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001073 int err, devlen = 0;
1074 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001075 .new_ino_d = ALIGN(devlen, 8),
1076 .dirtied_ino = 1 };
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001077 struct fscrypt_name nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001078
1079 /*
1080 * Budget request settings: new inode, new direntry and changing parent
1081 * directory inode.
1082 */
1083
Al Viro4cb2a012013-09-16 10:58:53 -04001084 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001085
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001086 if (S_ISBLK(mode) || S_ISCHR(mode)) {
1087 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1088 if (!dev)
1089 return -ENOMEM;
1090 devlen = ubifs_encode_dev(dev, rdev);
1091 }
1092
1093 err = ubifs_budget_space(c, &req);
1094 if (err) {
1095 kfree(dev);
1096 return err;
1097 }
1098
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001099 if (ubifs_crypt_is_encrypted(dir)) {
1100 err = fscrypt_get_encryption_info(dir);
1101 if (err)
1102 goto out_budg;
1103
1104 if (!fscrypt_has_encryption_key(dir)) {
1105 err = -EPERM;
1106 goto out_budg;
1107 }
1108 }
1109
1110 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1111 if (err)
1112 goto out_budg;
1113
1114 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1115
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001116 inode = ubifs_new_inode(c, dir, mode);
1117 if (IS_ERR(inode)) {
1118 kfree(dev);
1119 err = PTR_ERR(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001120 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001121 }
1122
1123 init_special_inode(inode, inode->i_mode, rdev);
1124 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
1125 ui = ubifs_inode(inode);
1126 ui->data = dev;
1127 ui->data_len = devlen;
1128
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001129 err = ubifs_init_security(dir, inode, &dentry->d_name);
1130 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001131 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001132
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001133 mutex_lock(&dir_ui->ui_mutex);
1134 dir->i_size += sz_change;
1135 dir_ui->ui_size = dir->i_size;
1136 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001137 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001138 if (err)
1139 goto out_cancel;
1140 mutex_unlock(&dir_ui->ui_mutex);
1141
1142 ubifs_release_budget(c, &req);
1143 insert_inode_hash(inode);
1144 d_instantiate(dentry, inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001145 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001146 return 0;
1147
1148out_cancel:
1149 dir->i_size -= sz_change;
1150 dir_ui->ui_size = dir->i_size;
1151 mutex_unlock(&dir_ui->ui_mutex);
Taesoo Kim9401a792015-03-25 09:53:37 +01001152out_inode:
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001153 make_bad_inode(inode);
1154 iput(inode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001155out_fname:
1156 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001157out_budg:
1158 ubifs_release_budget(c, &req);
1159 return err;
1160}
1161
1162static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
1163 const char *symname)
1164{
1165 struct inode *inode;
1166 struct ubifs_inode *ui;
1167 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1168 struct ubifs_info *c = dir->i_sb->s_fs_info;
1169 int err, len = strlen(symname);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001170 int sz_change = CALC_DENT_SIZE(len);
1171 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
1172 struct fscrypt_symlink_data *sd = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001173 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001174 .new_ino_d = ALIGN(len, 8),
1175 .dirtied_ino = 1 };
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001176 struct fscrypt_name nm;
1177
1178 if (ubifs_crypt_is_encrypted(dir)) {
1179 err = fscrypt_get_encryption_info(dir);
1180 if (err)
1181 goto out_budg;
1182
1183 if (!fscrypt_has_encryption_key(dir)) {
1184 err = -EPERM;
1185 goto out_budg;
1186 }
1187
1188 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
1189 sizeof(struct fscrypt_symlink_data));
1190 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001191
1192 /*
1193 * Budget request settings: new inode, new direntry and changing parent
1194 * directory inode.
1195 */
1196
Al Viro4cb2a012013-09-16 10:58:53 -04001197 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
1198 symname, dir->i_ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001199
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001200 if (disk_link.len > UBIFS_MAX_INO_DATA)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001201 return -ENAMETOOLONG;
1202
1203 err = ubifs_budget_space(c, &req);
1204 if (err)
1205 return err;
1206
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001207 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1208 if (err)
1209 goto out_budg;
1210
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001211 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
1212 if (IS_ERR(inode)) {
1213 err = PTR_ERR(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001214 goto out_fname;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001215 }
1216
1217 ui = ubifs_inode(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001218 ui->data = kmalloc(disk_link.len, GFP_NOFS);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001219 if (!ui->data) {
1220 err = -ENOMEM;
1221 goto out_inode;
1222 }
1223
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001224 if (ubifs_crypt_is_encrypted(dir)) {
1225 struct qstr istr = QSTR_INIT(symname, len);
1226 struct fscrypt_str ostr;
1227
1228 sd = kzalloc(disk_link.len, GFP_NOFS);
1229 if (!sd) {
1230 err = -ENOMEM;
1231 goto out_inode;
1232 }
1233
1234 err = fscrypt_get_encryption_info(inode);
1235 if (err) {
1236 kfree(sd);
1237 goto out_inode;
1238 }
1239
1240 if (!fscrypt_has_encryption_key(inode)) {
1241 kfree(sd);
1242 err = -EPERM;
1243 goto out_inode;
1244 }
1245
1246 ostr.name = sd->encrypted_path;
1247 ostr.len = disk_link.len;
1248
1249 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
1250 if (err) {
1251 kfree(sd);
1252 goto out_inode;
1253 }
1254
1255 sd->len = cpu_to_le16(ostr.len);
1256 disk_link.name = (char *)sd;
1257 } else {
1258 inode->i_link = ui->data;
1259 }
1260
1261 memcpy(ui->data, disk_link.name, disk_link.len);
1262 ((char *)ui->data)[disk_link.len - 1] = '\0';
1263
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001264 /*
1265 * The terminating zero byte is not written to the flash media and it
1266 * is put just to make later in-memory string processing simpler. Thus,
1267 * data length is @len, not @len + %1.
1268 */
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001269 ui->data_len = disk_link.len - 1;
1270 inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001271
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001272 err = ubifs_init_security(dir, inode, &dentry->d_name);
1273 if (err)
Taesoo Kim9401a792015-03-25 09:53:37 +01001274 goto out_inode;
Subodh Nijsured7f0b702014-10-31 13:50:30 -05001275
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001276 mutex_lock(&dir_ui->ui_mutex);
1277 dir->i_size += sz_change;
1278 dir_ui->ui_size = dir->i_size;
1279 dir->i_mtime = dir->i_ctime = inode->i_ctime;
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001280 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001281 if (err)
1282 goto out_cancel;
1283 mutex_unlock(&dir_ui->ui_mutex);
1284
1285 ubifs_release_budget(c, &req);
1286 insert_inode_hash(inode);
1287 d_instantiate(dentry, inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001288 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001289 return 0;
1290
1291out_cancel:
1292 dir->i_size -= sz_change;
1293 dir_ui->ui_size = dir->i_size;
1294 mutex_unlock(&dir_ui->ui_mutex);
1295out_inode:
1296 make_bad_inode(inode);
1297 iput(inode);
Richard Weinbergerca7f85b2016-10-08 13:24:26 +02001298out_fname:
1299 fscrypt_free_filename(&nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001300out_budg:
1301 ubifs_release_budget(c, &req);
1302 return err;
1303}
1304
1305/**
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001306 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001307 * @inode1: first inode
1308 * @inode2: second inode
1309 * @inode3: third inode
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001310 * @inode4: fouth inode
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001311 *
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001312 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001313 * @inode2 whereas @inode3 and @inode4 may be %NULL.
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001314 *
1315 * We do not implement any tricks to guarantee strict lock ordering, because
1316 * VFS has already done it for us on the @i_mutex. So this is just a simple
1317 * wrapper function.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001318 */
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001319static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1320 struct inode *inode3, struct inode *inode4)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001321{
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001322 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1323 if (inode2 != inode1)
1324 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1325 if (inode3)
1326 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001327 if (inode4)
1328 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001329}
1330
1331/**
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001332 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001333 * @inode1: first inode
1334 * @inode2: second inode
1335 * @inode3: third inode
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001336 * @inode4: fouth inode
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001337 */
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001338static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1339 struct inode *inode3, struct inode *inode4)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001340{
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001341 if (inode4)
1342 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001343 if (inode3)
1344 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001345 if (inode1 != inode2)
1346 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1347 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001348}
1349
Richard Weinberger390975a2016-10-18 21:21:43 +02001350static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1351 struct inode *new_dir, struct dentry *new_dentry,
1352 unsigned int flags)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001353{
1354 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +00001355 struct inode *old_inode = d_inode(old_dentry);
1356 struct inode *new_inode = d_inode(new_dentry);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001357 struct inode *whiteout = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001358 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001359 struct ubifs_inode *whiteout_ui = NULL;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001360 int err, release, sync = 0, move = (new_dir != old_dir);
1361 int is_dir = S_ISDIR(old_inode->i_mode);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001362 int unlink = !!new_inode, new_sz, old_sz;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001363 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1364 .dirtied_ino = 3 };
1365 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
Artem Bityutskiydab4b4d2008-07-24 14:52:45 +03001366 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001367 struct timespec time;
Alexandre Pereira da Silva782759b2012-06-25 17:47:49 -03001368 unsigned int uninitialized_var(saved_nlink);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001369 struct fscrypt_name old_nm, new_nm;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001370
Miklos Szeredif03b8ad2016-09-27 11:03:57 +02001371 if (flags & ~RENAME_NOREPLACE)
1372 return -EINVAL;
1373
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001374 /*
1375 * Budget request settings: deletion direntry, new direntry, removing
1376 * the old inode, and changing old and new parent directory inodes.
1377 *
1378 * However, this operation also marks the target inode as dirty and
1379 * does not write it, so we allocate budget for the target inode
1380 * separately.
1381 */
1382
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001383 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 -04001384 old_dentry, old_inode->i_ino, old_dir->i_ino,
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001385 new_dentry, new_dir->i_ino, flags);
1386
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001387 if (unlink)
Al Viro59551022016-01-22 15:40:57 -05001388 ubifs_assert(inode_is_locked(new_inode));
Artem Bityutskiy82c15932009-01-20 16:46:02 +02001389
Richard Weinbergerac7e47a2016-09-29 20:00:38 +02001390 if (old_dir != new_dir) {
1391 if (ubifs_crypt_is_encrypted(new_dir) &&
1392 !fscrypt_has_permitted_context(new_dir, old_inode))
1393 return -EPERM;
1394 }
1395
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001396 if (unlink && is_dir) {
Richard Weinbergerf6337d82016-09-19 20:54:19 +02001397 err = ubifs_check_dir_empty(new_inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001398 if (err)
1399 return err;
1400 }
1401
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001402 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001403 if (err)
1404 return err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001405
1406 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
1407 if (err) {
1408 fscrypt_free_filename(&old_nm);
1409 return err;
1410 }
1411
1412 new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
1413 old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
1414
1415 err = ubifs_budget_space(c, &req);
1416 if (err) {
1417 fscrypt_free_filename(&old_nm);
1418 fscrypt_free_filename(&new_nm);
1419 return err;
1420 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001421 err = ubifs_budget_space(c, &ino_req);
1422 if (err) {
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001423 fscrypt_free_filename(&old_nm);
1424 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001425 ubifs_release_budget(c, &req);
1426 return err;
1427 }
1428
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001429 if (flags & RENAME_WHITEOUT) {
1430 union ubifs_dev_desc *dev = NULL;
1431
1432 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1433 if (!dev) {
1434 ubifs_release_budget(c, &req);
1435 ubifs_release_budget(c, &ino_req);
1436 return -ENOMEM;
1437 }
1438
1439 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1440 if (err) {
1441 ubifs_release_budget(c, &req);
1442 ubifs_release_budget(c, &ino_req);
1443 kfree(dev);
1444 return err;
1445 }
1446
1447 whiteout->i_state |= I_LINKABLE;
1448 whiteout_ui = ubifs_inode(whiteout);
1449 whiteout_ui->data = dev;
1450 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
1451 ubifs_assert(!whiteout_ui->dirty);
1452 }
1453
1454 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001455
1456 /*
1457 * Like most other Unix systems, set the @i_ctime for inodes on a
1458 * rename.
1459 */
1460 time = ubifs_current_time(old_dir);
1461 old_inode->i_ctime = time;
1462
1463 /* We must adjust parent link count when renaming directories */
1464 if (is_dir) {
1465 if (move) {
1466 /*
1467 * @old_dir loses a link because we are moving
1468 * @old_inode to a different directory.
1469 */
1470 drop_nlink(old_dir);
1471 /*
1472 * @new_dir only gains a link if we are not also
1473 * overwriting an existing directory.
1474 */
1475 if (!unlink)
1476 inc_nlink(new_dir);
1477 } else {
1478 /*
1479 * @old_inode is not moving to a different directory,
1480 * but @old_dir still loses a link if we are
1481 * overwriting an existing directory.
1482 */
1483 if (unlink)
1484 drop_nlink(old_dir);
1485 }
1486 }
1487
1488 old_dir->i_size -= old_sz;
1489 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1490 old_dir->i_mtime = old_dir->i_ctime = time;
1491 new_dir->i_mtime = new_dir->i_ctime = time;
1492
1493 /*
1494 * And finally, if we unlinked a direntry which happened to have the
1495 * same name as the moved direntry, we have to decrement @i_nlink of
1496 * the unlinked inode and change its ctime.
1497 */
1498 if (unlink) {
1499 /*
1500 * Directories cannot have hard-links, so if this is a
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001501 * directory, just clear @i_nlink.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001502 */
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001503 saved_nlink = new_inode->i_nlink;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001504 if (is_dir)
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001505 clear_nlink(new_inode);
1506 else
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001507 drop_nlink(new_inode);
1508 new_inode->i_ctime = time;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001509 } else {
1510 new_dir->i_size += new_sz;
1511 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1512 }
1513
1514 /*
1515 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1516 * is dirty, because this will be done later on at the end of
1517 * 'ubifs_rename()'.
1518 */
1519 if (IS_SYNC(old_inode)) {
1520 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1521 if (unlink && IS_SYNC(new_inode))
1522 sync = 1;
1523 }
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001524
1525 if (whiteout) {
1526 struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1527 .dirtied_ino_d = \
1528 ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1529
1530 err = ubifs_budget_space(c, &wht_req);
1531 if (err) {
1532 ubifs_release_budget(c, &req);
1533 ubifs_release_budget(c, &ino_req);
1534 kfree(whiteout_ui->data);
1535 whiteout_ui->data_len = 0;
1536 iput(whiteout);
1537 return err;
1538 }
1539
1540 inc_nlink(whiteout);
1541 mark_inode_dirty(whiteout);
1542 whiteout->i_state &= ~I_LINKABLE;
1543 iput(whiteout);
1544 }
1545
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001546 err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
1547 new_inode, &new_nm, whiteout, sync);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001548 if (err)
1549 goto out_cancel;
1550
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001551 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001552 ubifs_release_budget(c, &req);
1553
1554 mutex_lock(&old_inode_ui->ui_mutex);
1555 release = old_inode_ui->dirty;
1556 mark_inode_dirty_sync(old_inode);
1557 mutex_unlock(&old_inode_ui->ui_mutex);
1558
1559 if (release)
1560 ubifs_release_budget(c, &ino_req);
1561 if (IS_SYNC(old_inode))
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001562 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001563
1564 fscrypt_free_filename(&old_nm);
1565 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001566 return err;
1567
1568out_cancel:
1569 if (unlink) {
Artem Bityutskiyc43be102012-02-07 10:58:51 +02001570 set_nlink(new_inode, saved_nlink);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001571 } else {
1572 new_dir->i_size -= new_sz;
1573 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1574 }
1575 old_dir->i_size += old_sz;
1576 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1577 if (is_dir) {
1578 if (move) {
1579 inc_nlink(old_dir);
1580 if (!unlink)
1581 drop_nlink(new_dir);
1582 } else {
1583 if (unlink)
1584 inc_nlink(old_dir);
1585 }
1586 }
Richard Weinberger9e0a1ff2016-09-14 22:28:50 +02001587 if (whiteout) {
1588 drop_nlink(whiteout);
1589 iput(whiteout);
1590 }
1591 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001592 ubifs_release_budget(c, &ino_req);
1593 ubifs_release_budget(c, &req);
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001594 fscrypt_free_filename(&old_nm);
1595 fscrypt_free_filename(&new_nm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001596 return err;
1597}
1598
Richard Weinberger9ec64962016-09-14 22:28:51 +02001599static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1600 struct inode *new_dir, struct dentry *new_dentry)
1601{
1602 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1603 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1604 .dirtied_ino = 2 };
1605 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1606 struct inode *fst_inode = d_inode(old_dentry);
1607 struct inode *snd_inode = d_inode(new_dentry);
1608 struct timespec time;
1609 int err;
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001610 struct fscrypt_name fst_nm, snd_nm;
Richard Weinberger9ec64962016-09-14 22:28:51 +02001611
1612 ubifs_assert(fst_inode && snd_inode);
1613
Richard Weinbergerac7e47a2016-09-29 20:00:38 +02001614 if ((ubifs_crypt_is_encrypted(old_dir) ||
1615 ubifs_crypt_is_encrypted(new_dir)) &&
1616 (old_dir != new_dir) &&
1617 (!fscrypt_has_permitted_context(new_dir, fst_inode) ||
1618 !fscrypt_has_permitted_context(old_dir, snd_inode)))
1619 return -EPERM;
1620
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001621 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
1622 if (err)
1623 return err;
1624
1625 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
1626 if (err) {
1627 fscrypt_free_filename(&fst_nm);
1628 return err;
1629 }
1630
Richard Weinberger9ec64962016-09-14 22:28:51 +02001631 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1632
1633 time = ubifs_current_time(old_dir);
1634 fst_inode->i_ctime = time;
1635 snd_inode->i_ctime = time;
1636 old_dir->i_mtime = old_dir->i_ctime = time;
1637 new_dir->i_mtime = new_dir->i_ctime = time;
1638
1639 if (old_dir != new_dir) {
1640 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1641 inc_nlink(new_dir);
1642 drop_nlink(old_dir);
1643 }
1644 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1645 drop_nlink(new_dir);
1646 inc_nlink(old_dir);
1647 }
1648 }
1649
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001650 err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
1651 snd_inode, &snd_nm, sync);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001652
1653 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1654 ubifs_release_budget(c, &req);
1655
Richard Weinbergerf4f61d22016-11-11 22:50:29 +01001656 fscrypt_free_filename(&fst_nm);
1657 fscrypt_free_filename(&snd_nm);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001658 return err;
1659}
1660
Richard Weinberger390975a2016-10-18 21:21:43 +02001661static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
Richard Weinberger9ec64962016-09-14 22:28:51 +02001662 struct inode *new_dir, struct dentry *new_dentry,
1663 unsigned int flags)
1664{
1665 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1666 return -EINVAL;
1667
1668 ubifs_assert(inode_is_locked(old_dir));
1669 ubifs_assert(inode_is_locked(new_dir));
1670
1671 if (flags & RENAME_EXCHANGE)
1672 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1673
Richard Weinberger390975a2016-10-18 21:21:43 +02001674 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Richard Weinberger9ec64962016-09-14 22:28:51 +02001675}
1676
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001677int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1678 struct kstat *stat)
1679{
1680 loff_t size;
David Howells2b0143b2015-03-17 22:25:59 +00001681 struct inode *inode = d_inode(dentry);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001682 struct ubifs_inode *ui = ubifs_inode(inode);
1683
1684 mutex_lock(&ui->ui_mutex);
Al Viro6d42e7e2012-04-02 14:25:07 -04001685 generic_fillattr(inode, stat);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001686 stat->blksize = UBIFS_BLOCK_SIZE;
1687 stat->size = ui->ui_size;
1688
1689 /*
1690 * Unfortunately, the 'stat()' system call was designed for block
1691 * device based file systems, and it is not appropriate for UBIFS,
1692 * because UBIFS does not have notion of "block". For example, it is
1693 * difficult to tell how many block a directory takes - it actually
1694 * takes less than 300 bytes, but we have to round it to block size,
1695 * which introduces large mistake. This makes utilities like 'du' to
1696 * report completely senseless numbers. This is the reason why UBIFS
1697 * goes the same way as JFFS2 - it reports zero blocks for everything
1698 * but regular files, which makes more sense than reporting completely
1699 * wrong sizes.
1700 */
1701 if (S_ISREG(inode->i_mode)) {
1702 size = ui->xattr_size;
1703 size += stat->size;
1704 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1705 /*
1706 * Note, user-space expects 512-byte blocks count irrespectively
1707 * of what was reported in @stat->size.
1708 */
1709 stat->blocks = size >> 9;
1710 } else
1711 stat->blocks = 0;
1712 mutex_unlock(&ui->ui_mutex);
1713 return 0;
1714}
1715
Richard Weinbergerba40e6a2016-09-29 17:20:27 +02001716static int ubifs_dir_open(struct inode *dir, struct file *file)
1717{
1718 if (ubifs_crypt_is_encrypted(dir))
1719 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1720
1721 return 0;
1722}
1723
Artem Bityutskiye8b81562009-01-15 17:43:23 +02001724const struct inode_operations ubifs_dir_inode_operations = {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001725 .lookup = ubifs_lookup,
1726 .create = ubifs_create,
1727 .link = ubifs_link,
1728 .symlink = ubifs_symlink,
1729 .unlink = ubifs_unlink,
1730 .mkdir = ubifs_mkdir,
1731 .rmdir = ubifs_rmdir,
1732 .mknod = ubifs_mknod,
Richard Weinberger390975a2016-10-18 21:21:43 +02001733 .rename = ubifs_rename,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001734 .setattr = ubifs_setattr,
1735 .getattr = ubifs_getattr,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001736 .listxattr = ubifs_listxattr,
Dongsheng Yang8c1c5f22015-11-07 12:46:11 +08001737#ifdef CONFIG_UBIFS_ATIME_SUPPORT
1738 .update_time = ubifs_update_time,
1739#endif
Richard Weinberger474b9372016-09-14 22:28:49 +02001740 .tmpfile = ubifs_tmpfile,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001741};
1742
Artem Bityutskiye8b81562009-01-15 17:43:23 +02001743const struct file_operations ubifs_dir_operations = {
Al Viro01122e02013-05-16 01:14:46 -04001744 .llseek = generic_file_llseek,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001745 .release = ubifs_dir_release,
1746 .read = generic_read_dir,
Al Viroc51da202016-04-30 22:37:34 -04001747 .iterate_shared = ubifs_readdir,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001748 .fsync = ubifs_fsync,
1749 .unlocked_ioctl = ubifs_ioctl,
Richard Weinbergerba40e6a2016-09-29 17:20:27 +02001750 .open = ubifs_dir_open,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001751#ifdef CONFIG_COMPAT
1752 .compat_ioctl = ubifs_compat_ioctl,
1753#endif
1754};