]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ubifs/dir.c
Merge branch 'etnaviv/fixes' of git://git.pengutronix.de/git/lst/linux
[mirror_ubuntu-bionic-kernel.git] / fs / ubifs / dir.c
CommitLineData
1e51764a
AB
1/* * This file is part of UBIFS.
2 *
3 * Copyright (C) 2006-2008 Nokia Corporation.
4 * Copyright (C) 2006, 2007 University of Szeged, Hungary
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 * Zoltan Sogor
22 */
23
24/*
25 * This file implements directory operations.
26 *
27 * All FS operations in this file allocate budget before writing anything to the
28 * media. If they fail to allocate it, the error is returned. The only
29 * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
30 * if they unable to allocate the budget, because deletion %-ENOSPC failure is
31 * not what users are usually ready to get. UBIFS budgeting subsystem has some
32 * space reserved for these purposes.
33 *
34 * All operations in this file write all inodes which they change straight
35 * away, instead of marking them dirty. For example, 'ubifs_link()' changes
36 * @i_size of the parent inode and writes the parent inode together with the
37 * target inode. This was done to simplify file-system recovery which would
38 * otherwise be very difficult to do. The only exception is rename which marks
39 * the re-named inode dirty (because its @i_ctime is updated) but does not
40 * write it, but just marks it as dirty.
41 */
42
43#include "ubifs.h"
44
45/**
46 * inherit_flags - inherit flags of the parent inode.
47 * @dir: parent inode
48 * @mode: new inode mode flags
49 *
50 * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
51 * parent directory inode @dir. UBIFS inodes inherit the following flags:
52 * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
53 * sub-directory basis;
54 * o %UBIFS_SYNC_FL - useful for the same reasons;
55 * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
56 *
57 * This function returns the inherited flags.
58 */
ad44be5c 59static int inherit_flags(const struct inode *dir, umode_t mode)
1e51764a
AB
60{
61 int flags;
62 const struct ubifs_inode *ui = ubifs_inode(dir);
63
64 if (!S_ISDIR(dir->i_mode))
65 /*
66 * The parent is not a directory, which means that an extended
67 * attribute inode is being created. No flags.
68 */
69 return 0;
70
71 flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
72 if (!S_ISDIR(mode))
73 /* The "DIRSYNC" flag only applies to directories */
74 flags &= ~UBIFS_DIRSYNC_FL;
75 return flags;
76}
77
78/**
79 * ubifs_new_inode - allocate new UBIFS inode object.
80 * @c: UBIFS file-system description object
81 * @dir: parent directory inode
82 * @mode: inode mode flags
83 *
84 * This function finds an unused inode number, allocates new inode and
85 * initializes it. Returns new inode in case of success and an error code in
86 * case of failure.
87 */
d475a507 88struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
ad44be5c 89 umode_t mode)
1e51764a 90{
d475a507 91 int err;
1e51764a
AB
92 struct inode *inode;
93 struct ubifs_inode *ui;
d475a507
RW
94 bool encrypted = false;
95
96 if (ubifs_crypt_is_encrypted(dir)) {
97 err = fscrypt_get_encryption_info(dir);
98 if (err) {
99 ubifs_err(c, "fscrypt_get_encryption_info failed: %i", err);
100 return ERR_PTR(err);
101 }
102
103 if (!fscrypt_has_encryption_key(dir))
104 return ERR_PTR(-EPERM);
105
106 encrypted = true;
107 }
1e51764a
AB
108
109 inode = new_inode(c->vfs_sb);
110 ui = ubifs_inode(inode);
111 if (!inode)
112 return ERR_PTR(-ENOMEM);
113
114 /*
115 * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
116 * marking them dirty in file write path (see 'file_update_time()').
117 * UBIFS has to fully control "clean <-> dirty" transitions of inodes
118 * to make budgeting work.
119 */
12e776a0 120 inode->i_flags |= S_NOCMTIME;
1e51764a 121
abf5d08a 122 inode_init_owner(inode, dir, mode);
1e51764a 123 inode->i_mtime = inode->i_atime = inode->i_ctime =
607a11ad 124 current_time(inode);
1e51764a 125 inode->i_mapping->nrpages = 0;
1e51764a
AB
126
127 switch (mode & S_IFMT) {
128 case S_IFREG:
129 inode->i_mapping->a_ops = &ubifs_file_address_operations;
130 inode->i_op = &ubifs_file_inode_operations;
131 inode->i_fop = &ubifs_file_operations;
132 break;
133 case S_IFDIR:
134 inode->i_op = &ubifs_dir_inode_operations;
135 inode->i_fop = &ubifs_dir_operations;
136 inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
137 break;
138 case S_IFLNK:
139 inode->i_op = &ubifs_symlink_inode_operations;
140 break;
141 case S_IFSOCK:
142 case S_IFIFO:
143 case S_IFBLK:
144 case S_IFCHR:
145 inode->i_op = &ubifs_file_inode_operations;
146 break;
147 default:
148 BUG();
149 }
150
151 ui->flags = inherit_flags(dir, mode);
152 ubifs_set_inode_flags(inode);
153 if (S_ISREG(mode))
154 ui->compr_type = c->default_compr;
155 else
156 ui->compr_type = UBIFS_COMPR_NONE;
157 ui->synced_i_size = 0;
158
159 spin_lock(&c->cnt_lock);
160 /* Inode number overflow is currently not supported */
161 if (c->highest_inum >= INUM_WARN_WATERMARK) {
162 if (c->highest_inum >= INUM_WATERMARK) {
163 spin_unlock(&c->cnt_lock);
235c362b 164 ubifs_err(c, "out of inode numbers");
1e51764a
AB
165 make_bad_inode(inode);
166 iput(inode);
167 return ERR_PTR(-EINVAL);
168 }
1a7e985d 169 ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
e84461ad 170 (unsigned long)c->highest_inum, INUM_WATERMARK);
1e51764a
AB
171 }
172
173 inode->i_ino = ++c->highest_inum;
1e51764a
AB
174 /*
175 * The creation sequence number remains with this inode for its
176 * lifetime. All nodes for this inode have a greater sequence number,
177 * and so it is possible to distinguish obsolete nodes belonging to a
178 * previous incarnation of the same inode number - for example, for the
179 * purpose of rebuilding the index.
180 */
181 ui->creat_sqnum = ++c->max_sqnum;
182 spin_unlock(&c->cnt_lock);
d475a507
RW
183
184 if (encrypted) {
185 err = fscrypt_inherit_context(dir, inode, &encrypted, true);
186 if (err) {
187 ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
188 make_bad_inode(inode);
189 iput(inode);
190 return ERR_PTR(err);
191 }
192 }
193
1e51764a
AB
194 return inode;
195}
196
bb2615d4
AB
197static int dbg_check_name(const struct ubifs_info *c,
198 const struct ubifs_dent_node *dent,
f4f61d2c 199 const struct fscrypt_name *nm)
1e51764a 200{
2b1844a8 201 if (!dbg_is_chk_gen(c))
1e51764a 202 return 0;
f4f61d2c 203 if (le16_to_cpu(dent->nlen) != fname_len(nm))
1e51764a 204 return -EINVAL;
f4f61d2c 205 if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
1e51764a
AB
206 return -EINVAL;
207 return 0;
208}
209
1e51764a 210static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 211 unsigned int flags)
1e51764a
AB
212{
213 int err;
214 union ubifs_key key;
215 struct inode *inode = NULL;
216 struct ubifs_dent_node *dent;
217 struct ubifs_info *c = dir->i_sb->s_fs_info;
f4f61d2c 218 struct fscrypt_name nm;
1e51764a 219
4cb2a01d 220 dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
1e51764a 221
9270b2f4
RW
222 if (ubifs_crypt_is_encrypted(dir)) {
223 err = fscrypt_get_encryption_info(dir);
224
225 /*
226 * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
227 * created while the directory was encrypted and we
228 * have access to the key.
229 */
230 if (fscrypt_has_encryption_key(dir))
231 fscrypt_set_encrypted_dentry(dentry);
232 fscrypt_set_d_op(dentry);
233 if (err && err != -ENOKEY)
234 return ERR_PTR(err);
235 }
236
f4f61d2c
RW
237 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
238 if (err)
239 return ERR_PTR(err);
240
241 if (fname_len(&nm) > UBIFS_MAX_NLEN) {
242 err = -ENAMETOOLONG;
243 goto out_fname;
244 }
1e51764a
AB
245
246 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
f4f61d2c
RW
247 if (!dent) {
248 err = -ENOMEM;
249 goto out_fname;
250 }
1e51764a 251
f4f61d2c
RW
252 if (nm.hash) {
253 ubifs_assert(fname_len(&nm) == 0);
254 ubifs_assert(fname_name(&nm) == NULL);
255 dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
528e3d17 256 err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
f4f61d2c
RW
257 } else {
258 dent_key_init(c, &key, dir->i_ino, &nm);
259 err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
260 }
1e51764a 261
1e51764a 262 if (err) {
720b499c 263 if (err == -ENOENT) {
1e51764a
AB
264 dbg_gen("not found");
265 goto done;
266 }
f4f61d2c 267 goto out_dent;
1e51764a
AB
268 }
269
f4f61d2c 270 if (dbg_check_name(c, dent, &nm)) {
1e51764a 271 err = -EINVAL;
f4f61d2c 272 goto out_dent;
1e51764a
AB
273 }
274
275 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
276 if (IS_ERR(inode)) {
277 /*
278 * This should not happen. Probably the file-system needs
279 * checking.
280 */
281 err = PTR_ERR(inode);
235c362b 282 ubifs_err(c, "dead directory entry '%pd', error %d",
4cb2a01d 283 dentry, err);
1e51764a 284 ubifs_ro_mode(c, err);
f4f61d2c 285 goto out_dent;
1e51764a
AB
286 }
287
413d5a9e
EB
288 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
1e51764a
AB
297done:
298 kfree(dent);
f4f61d2c 299 fscrypt_free_filename(&nm);
1e51764a
AB
300 /*
301 * Note, d_splice_alias() would be required instead if we supported
302 * NFS.
303 */
304 d_add(dentry, inode);
305 return NULL;
306
413d5a9e
EB
307out_inode:
308 iput(inode);
f4f61d2c 309out_dent:
1e51764a 310 kfree(dent);
f4f61d2c
RW
311out_fname:
312 fscrypt_free_filename(&nm);
1e51764a
AB
313 return ERR_PTR(err);
314}
315
4acdaf27 316static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 317 bool excl)
1e51764a
AB
318{
319 struct inode *inode;
320 struct ubifs_info *c = dir->i_sb->s_fs_info;
1e51764a
AB
321 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
322 .dirtied_ino = 1 };
323 struct ubifs_inode *dir_ui = ubifs_inode(dir);
f4f61d2c
RW
324 struct fscrypt_name nm;
325 int err, sz_change;
1e51764a
AB
326
327 /*
328 * Budget request settings: new inode, new direntry, changing the
329 * parent directory inode.
330 */
331
4cb2a01d
AV
332 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
333 dentry, mode, dir->i_ino);
1e51764a
AB
334
335 err = ubifs_budget_space(c, &req);
336 if (err)
337 return err;
338
f4f61d2c
RW
339 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
1e51764a
AB
345 inode = ubifs_new_inode(c, dir, mode);
346 if (IS_ERR(inode)) {
347 err = PTR_ERR(inode);
f4f61d2c 348 goto out_fname;
1e51764a
AB
349 }
350
d7f0b70d
SN
351 err = ubifs_init_security(dir, inode, &dentry->d_name);
352 if (err)
9401a795 353 goto out_inode;
d7f0b70d 354
1e51764a
AB
355 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;
f4f61d2c 359 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a
AB
360 if (err)
361 goto out_cancel;
362 mutex_unlock(&dir_ui->ui_mutex);
363
364 ubifs_release_budget(c, &req);
f4f61d2c 365 fscrypt_free_filename(&nm);
1e51764a
AB
366 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);
9401a795 374out_inode:
1e51764a
AB
375 make_bad_inode(inode);
376 iput(inode);
f4f61d2c
RW
377out_fname:
378 fscrypt_free_filename(&nm);
1e51764a
AB
379out_budg:
380 ubifs_release_budget(c, &req);
235c362b 381 ubifs_err(c, "cannot create regular file, error %d", err);
1e51764a
AB
382 return err;
383}
384
9e0a1fff
RW
385static int do_tmpfile(struct inode *dir, struct dentry *dentry,
386 umode_t mode, struct inode **whiteout)
474b9370
RW
387{
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;
f4f61d2c 394 struct fscrypt_name nm;
474b9370
RW
395
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
f4f61d2c 404 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
474b9370
RW
405 if (err)
406 return err;
407
f4f61d2c
RW
408 err = ubifs_budget_space(c, &req);
409 if (err) {
410 fscrypt_free_filename(&nm);
411 return err;
412 }
413
474b9370
RW
414 err = ubifs_budget_space(c, &ino_req);
415 if (err) {
416 ubifs_release_budget(c, &req);
f4f61d2c 417 fscrypt_free_filename(&nm);
474b9370
RW
418 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
9e0a1fff
RW
428 if (whiteout) {
429 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
430 ubifs_assert(inode->i_op == &ubifs_file_inode_operations);
431 }
432
474b9370
RW
433 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);
9e0a1fff
RW
439
440 if (whiteout) {
441 mark_inode_dirty(inode);
442 drop_nlink(inode);
443 *whiteout = inode;
444 } else {
445 d_tmpfile(dentry, inode);
446 }
474b9370 447 ubifs_assert(ui->dirty);
9e0a1fff 448
474b9370
RW
449 instantiated = 1;
450 mutex_unlock(&ui->ui_mutex);
451
452 mutex_lock(&dir_ui->ui_mutex);
f4f61d2c 453 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
474b9370
RW
454 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);
f4f61d2c 472 fscrypt_free_filename(&nm);
474b9370
RW
473 ubifs_err(c, "cannot create temporary file, error %d", err);
474 return err;
475}
476
9e0a1fff
RW
477static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
478 umode_t mode)
479{
480 return do_tmpfile(dir, dentry, mode, NULL);
481}
482
1e51764a
AB
483/**
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 */
01122e06 530static int ubifs_readdir(struct file *file, struct dir_context *ctx)
1e51764a 531{
ba75d570 532 int fstr_real_len = 0, err = 0;
f4f61d2c
RW
533 struct fscrypt_name nm;
534 struct fscrypt_str fstr = {0};
1e51764a
AB
535 union ubifs_key key;
536 struct ubifs_dent_node *dent;
496ad9aa 537 struct inode *dir = file_inode(file);
1e51764a 538 struct ubifs_info *c = dir->i_sb->s_fs_info;
f4f61d2c 539 bool encrypted = ubifs_crypt_is_encrypted(dir);
1e51764a 540
01122e06 541 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
1e51764a 542
01122e06 543 if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
1e51764a
AB
544 /*
545 * The directory was seek'ed to a senseless position or there
546 * are no more entries.
547 */
548 return 0;
549
f4f61d2c
RW
550 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
605c912b
AB
562 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
1e51764a 580 /* File positions 0 and 1 correspond to "." and ".." */
01122e06 581 if (ctx->pos < 2) {
1e51764a 582 ubifs_assert(!file->private_data);
f4f61d2c
RW
583 if (!dir_emit_dots(file, ctx)) {
584 if (encrypted)
585 fscrypt_fname_free_buffer(&fstr);
1e51764a 586 return 0;
f4f61d2c 587 }
1e51764a
AB
588
589 /* Find the first entry in TNC and save it */
590 lowest_dent_key(c, &key, dir->i_ino);
f4f61d2c 591 fname_len(&nm) = 0;
1e51764a
AB
592 dent = ubifs_tnc_next_ent(c, &key, &nm);
593 if (IS_ERR(dent)) {
594 err = PTR_ERR(dent);
595 goto out;
596 }
597
01122e06 598 ctx->pos = key_hash_flash(c, &dent->key);
1e51764a
AB
599 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.
01122e06 606 * Find the entry corresponding to @ctx->pos or the closest one.
1e51764a 607 */
01122e06 608 dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
f4f61d2c 609 fname_len(&nm) = 0;
1e51764a
AB
610 dent = ubifs_tnc_next_ent(c, &key, &nm);
611 if (IS_ERR(dent)) {
612 err = PTR_ERR(dent);
613 goto out;
614 }
01122e06 615 ctx->pos = key_hash_flash(c, &dent->key);
1e51764a
AB
616 file->private_data = dent;
617 }
618
619 while (1) {
b20e2d99
HL
620 dbg_gen("ino %llu, new f_pos %#x",
621 (unsigned long long)le64_to_cpu(dent->inum),
1e51764a 622 key_hash_flash(c, &dent->key));
0ecb9529
HH
623 ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
624 ubifs_inode(dir)->creat_sqnum);
1e51764a 625
f4f61d2c
RW
626 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
528e3d17
RW
632 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);
ca7f85be 636 if (err)
f4f61d2c
RW
637 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,
1e51764a 644 le64_to_cpu(dent->inum),
f4f61d2c
RW
645 vfs_dent_type(dent->type))) {
646 if (encrypted)
647 fscrypt_fname_free_buffer(&fstr);
1e51764a 648 return 0;
f4f61d2c 649 }
1e51764a
AB
650
651 /* Switch to the next entry */
652 key_read(c, &dent->key, &key);
1e51764a
AB
653 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);
01122e06 660 ctx->pos = key_hash_flash(c, &dent->key);
1e51764a
AB
661 file->private_data = dent;
662 cond_resched();
663 }
664
665out:
aeeb14f7
RW
666 kfree(file->private_data);
667 file->private_data = NULL;
668
f4f61d2c
RW
669 if (encrypted)
670 fscrypt_fname_free_buffer(&fstr);
671
c83ed4c9 672 if (err != -ENOENT)
235c362b 673 ubifs_err(c, "cannot find next direntry, error %d", err);
a00052a2
RW
674 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
1e51764a 682
605c912b 683 /* 2 is a special value indicating that there are no more direntries */
01122e06 684 ctx->pos = 2;
c83ed4c9 685 return err;
1e51764a
AB
686}
687
1e51764a
AB
688/* 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/**
82c1593c 697 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
1e51764a
AB
698 * @inode1: first inode
699 * @inode2: second inode
82c1593c
AB
700 *
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.
1e51764a
AB
704 */
705static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
706{
82c1593c
AB
707 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
708 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1e51764a
AB
709}
710
711/**
82c1593c 712 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
1e51764a
AB
713 * @inode1: first inode
714 * @inode2: second inode
715 */
716static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
717{
1e51764a 718 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
82c1593c 719 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1e51764a
AB
720}
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;
2b0143b5 726 struct inode *inode = d_inode(old_dentry);
1e51764a
AB
727 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,
dab4b4d2 731 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
f4f61d2c 732 struct fscrypt_name nm;
1e51764a
AB
733
734 /*
735 * Budget request settings: new direntry, changing the target inode,
736 * changing the parent inode.
737 */
738
4cb2a01d
AV
739 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
740 dentry, inode->i_ino,
1e51764a 741 inode->i_nlink, dir->i_ino);
5955102c
AV
742 ubifs_assert(inode_is_locked(dir));
743 ubifs_assert(inode_is_locked(inode));
8b3884a8 744
3d4b2fcb
EB
745 if (ubifs_crypt_is_encrypted(dir) &&
746 !fscrypt_has_permitted_context(dir, inode))
747 return -EPERM;
f4f61d2c
RW
748
749 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1e51764a
AB
750 if (err)
751 return err;
752
f4f61d2c
RW
753 err = dbg_check_synced_i_size(c, inode);
754 if (err)
755 goto out_fname;
756
1e51764a
AB
757 err = ubifs_budget_space(c, &req);
758 if (err)
f4f61d2c 759 goto out_fname;
1e51764a
AB
760
761 lock_2_inodes(dir, inode);
32fe905c
RW
762
763 /* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
764 if (inode->i_nlink == 0)
765 ubifs_delete_orphan(c, inode->i_ino);
766
1e51764a 767 inc_nlink(inode);
7de9c6ee 768 ihold(inode);
607a11ad 769 inode->i_ctime = current_time(inode);
1e51764a
AB
770 dir->i_size += sz_change;
771 dir_ui->ui_size = dir->i_size;
772 dir->i_mtime = dir->i_ctime = inode->i_ctime;
f4f61d2c 773 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a
AB
774 if (err)
775 goto out_cancel;
776 unlock_2_inodes(dir, inode);
777
778 ubifs_release_budget(c, &req);
779 d_instantiate(dentry, inode);
f4f61d2c 780 fscrypt_free_filename(&nm);
1e51764a
AB
781 return 0;
782
783out_cancel:
784 dir->i_size -= sz_change;
785 dir_ui->ui_size = dir->i_size;
786 drop_nlink(inode);
32fe905c
RW
787 if (inode->i_nlink == 0)
788 ubifs_add_orphan(c, inode->i_ino);
1e51764a
AB
789 unlock_2_inodes(dir, inode);
790 ubifs_release_budget(c, &req);
791 iput(inode);
f4f61d2c
RW
792out_fname:
793 fscrypt_free_filename(&nm);
1e51764a
AB
794 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;
2b0143b5 800 struct inode *inode = d_inode(dentry);
1e51764a 801 struct ubifs_inode *dir_ui = ubifs_inode(dir);
f4f61d2c 802 int err, sz_change, budgeted = 1;
1e51764a 803 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
c43be108 804 unsigned int saved_nlink = inode->i_nlink;
f4f61d2c 805 struct fscrypt_name nm;
1e51764a
AB
806
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
4cb2a01d
AV
814 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
815 dentry, inode->i_ino,
1e51764a 816 inode->i_nlink, dir->i_ino);
f4f61d2c
RW
817
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
5955102c
AV
830 ubifs_assert(inode_is_locked(dir));
831 ubifs_assert(inode_is_locked(inode));
d808efb4 832 err = dbg_check_synced_i_size(c, inode);
1e51764a 833 if (err)
f4f61d2c 834 goto out_fname;
1e51764a
AB
835
836 err = ubifs_budget_space(c, &req);
837 if (err) {
838 if (err != -ENOSPC)
f4f61d2c 839 goto out_fname;
1e51764a
AB
840 budgeted = 0;
841 }
842
843 lock_2_inodes(dir, inode);
607a11ad 844 inode->i_ctime = current_time(dir);
1e51764a
AB
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;
f4f61d2c 849 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
1e51764a
AB
850 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 */
b137545c 858 c->bi.nospace = c->bi.nospace_rp = 0;
1e51764a
AB
859 smp_wmb();
860 }
f4f61d2c 861 fscrypt_free_filename(&nm);
1e51764a
AB
862 return 0;
863
864out_cancel:
865 dir->i_size += sz_change;
866 dir_ui->ui_size = dir->i_size;
c43be108 867 set_nlink(inode, saved_nlink);
1e51764a
AB
868 unlock_2_inodes(dir, inode);
869 if (budgeted)
870 ubifs_release_budget(c, &req);
f4f61d2c
RW
871out_fname:
872 fscrypt_free_filename(&nm);
1e51764a
AB
873 return err;
874}
875
876/**
877 * check_dir_empty - check if a directory is empty or not.
1e51764a
AB
878 * @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 */
f6337d84 884int ubifs_check_dir_empty(struct inode *dir)
1e51764a 885{
f6337d84 886 struct ubifs_info *c = dir->i_sb->s_fs_info;
f4f61d2c 887 struct fscrypt_name nm = { 0 };
1e51764a
AB
888 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;
2b0143b5 908 struct inode *inode = d_inode(dentry);
f4f61d2c 909 int err, sz_change, budgeted = 1;
1e51764a
AB
910 struct ubifs_inode *dir_ui = ubifs_inode(dir);
911 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
f4f61d2c 912 struct fscrypt_name nm;
1e51764a
AB
913
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
4cb2a01d
AV
920 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
921 inode->i_ino, dir->i_ino);
5955102c
AV
922 ubifs_assert(inode_is_locked(dir));
923 ubifs_assert(inode_is_locked(inode));
f6337d84 924 err = ubifs_check_dir_empty(d_inode(dentry));
1e51764a
AB
925 if (err)
926 return err;
927
f4f61d2c
RW
928 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
1e51764a
AB
940 err = ubifs_budget_space(c, &req);
941 if (err) {
942 if (err != -ENOSPC)
f4f61d2c 943 goto out_fname;
1e51764a
AB
944 budgeted = 0;
945 }
946
947 lock_2_inodes(dir, inode);
607a11ad 948 inode->i_ctime = current_time(dir);
1e51764a
AB
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;
f4f61d2c 954 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
1e51764a
AB
955 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 */
b137545c 963 c->bi.nospace = c->bi.nospace_rp = 0;
1e51764a
AB
964 smp_wmb();
965 }
f4f61d2c 966 fscrypt_free_filename(&nm);
1e51764a
AB
967 return 0;
968
969out_cancel:
970 dir->i_size += sz_change;
971 dir_ui->ui_size = dir->i_size;
972 inc_nlink(dir);
c43be108 973 set_nlink(inode, 2);
1e51764a
AB
974 unlock_2_inodes(dir, inode);
975 if (budgeted)
976 ubifs_release_budget(c, &req);
f4f61d2c
RW
977out_fname:
978 fscrypt_free_filename(&nm);
1e51764a
AB
979 return err;
980}
981
18bb1db3 982static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1e51764a
AB
983{
984 struct inode *inode;
985 struct ubifs_inode *dir_ui = ubifs_inode(dir);
986 struct ubifs_info *c = dir->i_sb->s_fs_info;
f4f61d2c 987 int err, sz_change;
182854b4 988 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
f4f61d2c 989 struct fscrypt_name nm;
1e51764a
AB
990
991 /*
992 * Budget request settings: new inode, new direntry and changing parent
993 * directory inode.
994 */
995
4cb2a01d
AV
996 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
997 dentry, mode, dir->i_ino);
1e51764a
AB
998
999 err = ubifs_budget_space(c, &req);
1000 if (err)
1001 return err;
1002
f4f61d2c
RW
1003 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1004 if (err)
1005 goto out_budg;
1006
1007 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1008
1e51764a
AB
1009 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
1010 if (IS_ERR(inode)) {
1011 err = PTR_ERR(inode);
f4f61d2c 1012 goto out_fname;
1e51764a
AB
1013 }
1014
d7f0b70d
SN
1015 err = ubifs_init_security(dir, inode, &dentry->d_name);
1016 if (err)
9401a795 1017 goto out_inode;
d7f0b70d 1018
1e51764a
AB
1019 mutex_lock(&dir_ui->ui_mutex);
1020 insert_inode_hash(inode);
1021 inc_nlink(inode);
1022 inc_nlink(dir);
1023 dir->i_size += sz_change;
1024 dir_ui->ui_size = dir->i_size;
1025 dir->i_mtime = dir->i_ctime = inode->i_ctime;
f4f61d2c 1026 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a 1027 if (err) {
235c362b 1028 ubifs_err(c, "cannot create directory, error %d", err);
1e51764a
AB
1029 goto out_cancel;
1030 }
1031 mutex_unlock(&dir_ui->ui_mutex);
1032
1033 ubifs_release_budget(c, &req);
1034 d_instantiate(dentry, inode);
f4f61d2c 1035 fscrypt_free_filename(&nm);
1e51764a
AB
1036 return 0;
1037
1038out_cancel:
1039 dir->i_size -= sz_change;
1040 dir_ui->ui_size = dir->i_size;
1041 drop_nlink(dir);
1042 mutex_unlock(&dir_ui->ui_mutex);
9401a795 1043out_inode:
1e51764a
AB
1044 make_bad_inode(inode);
1045 iput(inode);
f4f61d2c
RW
1046out_fname:
1047 fscrypt_free_filename(&nm);
1e51764a
AB
1048out_budg:
1049 ubifs_release_budget(c, &req);
1050 return err;
1051}
1052
1053static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
1a67aafb 1054 umode_t mode, dev_t rdev)
1e51764a
AB
1055{
1056 struct inode *inode;
1057 struct ubifs_inode *ui;
1058 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1059 struct ubifs_info *c = dir->i_sb->s_fs_info;
1060 union ubifs_dev_desc *dev = NULL;
f4f61d2c 1061 int sz_change;
1e51764a
AB
1062 int err, devlen = 0;
1063 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
dab4b4d2
AB
1064 .new_ino_d = ALIGN(devlen, 8),
1065 .dirtied_ino = 1 };
f4f61d2c 1066 struct fscrypt_name nm;
1e51764a
AB
1067
1068 /*
1069 * Budget request settings: new inode, new direntry and changing parent
1070 * directory inode.
1071 */
1072
4cb2a01d 1073 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
1e51764a 1074
1e51764a
AB
1075 if (S_ISBLK(mode) || S_ISCHR(mode)) {
1076 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1077 if (!dev)
1078 return -ENOMEM;
1079 devlen = ubifs_encode_dev(dev, rdev);
1080 }
1081
1082 err = ubifs_budget_space(c, &req);
1083 if (err) {
1084 kfree(dev);
1085 return err;
1086 }
1087
f4f61d2c 1088 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
63ed6573
RW
1089 if (err) {
1090 kfree(dev);
f4f61d2c 1091 goto out_budg;
63ed6573 1092 }
f4f61d2c
RW
1093
1094 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1095
1e51764a
AB
1096 inode = ubifs_new_inode(c, dir, mode);
1097 if (IS_ERR(inode)) {
1098 kfree(dev);
1099 err = PTR_ERR(inode);
f4f61d2c 1100 goto out_fname;
1e51764a
AB
1101 }
1102
1103 init_special_inode(inode, inode->i_mode, rdev);
1104 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
1105 ui = ubifs_inode(inode);
1106 ui->data = dev;
1107 ui->data_len = devlen;
1108
d7f0b70d
SN
1109 err = ubifs_init_security(dir, inode, &dentry->d_name);
1110 if (err)
9401a795 1111 goto out_inode;
d7f0b70d 1112
1e51764a
AB
1113 mutex_lock(&dir_ui->ui_mutex);
1114 dir->i_size += sz_change;
1115 dir_ui->ui_size = dir->i_size;
1116 dir->i_mtime = dir->i_ctime = inode->i_ctime;
f4f61d2c 1117 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a
AB
1118 if (err)
1119 goto out_cancel;
1120 mutex_unlock(&dir_ui->ui_mutex);
1121
1122 ubifs_release_budget(c, &req);
1123 insert_inode_hash(inode);
1124 d_instantiate(dentry, inode);
f4f61d2c 1125 fscrypt_free_filename(&nm);
1e51764a
AB
1126 return 0;
1127
1128out_cancel:
1129 dir->i_size -= sz_change;
1130 dir_ui->ui_size = dir->i_size;
1131 mutex_unlock(&dir_ui->ui_mutex);
9401a795 1132out_inode:
1e51764a
AB
1133 make_bad_inode(inode);
1134 iput(inode);
f4f61d2c
RW
1135out_fname:
1136 fscrypt_free_filename(&nm);
1e51764a
AB
1137out_budg:
1138 ubifs_release_budget(c, &req);
1139 return err;
1140}
1141
1142static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
1143 const char *symname)
1144{
1145 struct inode *inode;
1146 struct ubifs_inode *ui;
1147 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1148 struct ubifs_info *c = dir->i_sb->s_fs_info;
1149 int err, len = strlen(symname);
ca7f85be
RW
1150 int sz_change = CALC_DENT_SIZE(len);
1151 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
1152 struct fscrypt_symlink_data *sd = NULL;
1e51764a 1153 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
dab4b4d2
AB
1154 .new_ino_d = ALIGN(len, 8),
1155 .dirtied_ino = 1 };
ca7f85be
RW
1156 struct fscrypt_name nm;
1157
1158 if (ubifs_crypt_is_encrypted(dir)) {
1159 err = fscrypt_get_encryption_info(dir);
1160 if (err)
1161 goto out_budg;
1162
1163 if (!fscrypt_has_encryption_key(dir)) {
1164 err = -EPERM;
1165 goto out_budg;
1166 }
1167
1168 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
1169 sizeof(struct fscrypt_symlink_data));
1170 }
1e51764a
AB
1171
1172 /*
1173 * Budget request settings: new inode, new direntry and changing parent
1174 * directory inode.
1175 */
1176
4cb2a01d
AV
1177 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
1178 symname, dir->i_ino);
1e51764a 1179
ca7f85be 1180 if (disk_link.len > UBIFS_MAX_INO_DATA)
1e51764a
AB
1181 return -ENAMETOOLONG;
1182
1183 err = ubifs_budget_space(c, &req);
1184 if (err)
1185 return err;
1186
ca7f85be
RW
1187 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1188 if (err)
1189 goto out_budg;
1190
1e51764a
AB
1191 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
1192 if (IS_ERR(inode)) {
1193 err = PTR_ERR(inode);
ca7f85be 1194 goto out_fname;
1e51764a
AB
1195 }
1196
1197 ui = ubifs_inode(inode);
ca7f85be 1198 ui->data = kmalloc(disk_link.len, GFP_NOFS);
1e51764a
AB
1199 if (!ui->data) {
1200 err = -ENOMEM;
1201 goto out_inode;
1202 }
1203
ca7f85be
RW
1204 if (ubifs_crypt_is_encrypted(dir)) {
1205 struct qstr istr = QSTR_INIT(symname, len);
1206 struct fscrypt_str ostr;
1207
1208 sd = kzalloc(disk_link.len, GFP_NOFS);
1209 if (!sd) {
1210 err = -ENOMEM;
1211 goto out_inode;
1212 }
1213
ca7f85be
RW
1214 ostr.name = sd->encrypted_path;
1215 ostr.len = disk_link.len;
1216
1217 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
1218 if (err) {
1219 kfree(sd);
1220 goto out_inode;
1221 }
1222
1223 sd->len = cpu_to_le16(ostr.len);
1224 disk_link.name = (char *)sd;
1225 } else {
1226 inode->i_link = ui->data;
1227 }
1228
1229 memcpy(ui->data, disk_link.name, disk_link.len);
1230 ((char *)ui->data)[disk_link.len - 1] = '\0';
1231
1e51764a
AB
1232 /*
1233 * The terminating zero byte is not written to the flash media and it
1234 * is put just to make later in-memory string processing simpler. Thus,
1235 * data length is @len, not @len + %1.
1236 */
ca7f85be
RW
1237 ui->data_len = disk_link.len - 1;
1238 inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
1e51764a 1239
d7f0b70d
SN
1240 err = ubifs_init_security(dir, inode, &dentry->d_name);
1241 if (err)
9401a795 1242 goto out_inode;
d7f0b70d 1243
1e51764a
AB
1244 mutex_lock(&dir_ui->ui_mutex);
1245 dir->i_size += sz_change;
1246 dir_ui->ui_size = dir->i_size;
1247 dir->i_mtime = dir->i_ctime = inode->i_ctime;
ca7f85be 1248 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a
AB
1249 if (err)
1250 goto out_cancel;
1251 mutex_unlock(&dir_ui->ui_mutex);
1252
1253 ubifs_release_budget(c, &req);
1254 insert_inode_hash(inode);
1255 d_instantiate(dentry, inode);
ca7f85be 1256 fscrypt_free_filename(&nm);
1e51764a
AB
1257 return 0;
1258
1259out_cancel:
1260 dir->i_size -= sz_change;
1261 dir_ui->ui_size = dir->i_size;
1262 mutex_unlock(&dir_ui->ui_mutex);
1263out_inode:
1264 make_bad_inode(inode);
1265 iput(inode);
ca7f85be
RW
1266out_fname:
1267 fscrypt_free_filename(&nm);
1e51764a
AB
1268out_budg:
1269 ubifs_release_budget(c, &req);
1270 return err;
1271}
1272
1273/**
9e0a1fff 1274 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1e51764a
AB
1275 * @inode1: first inode
1276 * @inode2: second inode
1277 * @inode3: third inode
9e0a1fff 1278 * @inode4: fouth inode
1e51764a 1279 *
82c1593c 1280 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
9e0a1fff 1281 * @inode2 whereas @inode3 and @inode4 may be %NULL.
82c1593c
AB
1282 *
1283 * We do not implement any tricks to guarantee strict lock ordering, because
1284 * VFS has already done it for us on the @i_mutex. So this is just a simple
1285 * wrapper function.
1e51764a 1286 */
9e0a1fff
RW
1287static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1288 struct inode *inode3, struct inode *inode4)
1e51764a 1289{
82c1593c
AB
1290 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1291 if (inode2 != inode1)
1292 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1293 if (inode3)
1294 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
9e0a1fff
RW
1295 if (inode4)
1296 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
1e51764a
AB
1297}
1298
1299/**
9e0a1fff 1300 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1e51764a
AB
1301 * @inode1: first inode
1302 * @inode2: second inode
1303 * @inode3: third inode
9e0a1fff 1304 * @inode4: fouth inode
1e51764a 1305 */
9e0a1fff
RW
1306static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1307 struct inode *inode3, struct inode *inode4)
1e51764a 1308{
9e0a1fff
RW
1309 if (inode4)
1310 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
1e51764a
AB
1311 if (inode3)
1312 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
82c1593c
AB
1313 if (inode1 != inode2)
1314 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1315 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1e51764a
AB
1316}
1317
390975ac
RW
1318static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1319 struct inode *new_dir, struct dentry *new_dentry,
1320 unsigned int flags)
1e51764a
AB
1321{
1322 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
2b0143b5
DH
1323 struct inode *old_inode = d_inode(old_dentry);
1324 struct inode *new_inode = d_inode(new_dentry);
9e0a1fff 1325 struct inode *whiteout = NULL;
1e51764a 1326 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
9e0a1fff 1327 struct ubifs_inode *whiteout_ui = NULL;
1e51764a
AB
1328 int err, release, sync = 0, move = (new_dir != old_dir);
1329 int is_dir = S_ISDIR(old_inode->i_mode);
f4f61d2c 1330 int unlink = !!new_inode, new_sz, old_sz;
1e51764a
AB
1331 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1332 .dirtied_ino = 3 };
1333 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
dab4b4d2 1334 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
1e51764a 1335 struct timespec time;
782759b9 1336 unsigned int uninitialized_var(saved_nlink);
f4f61d2c 1337 struct fscrypt_name old_nm, new_nm;
1e51764a
AB
1338
1339 /*
1340 * Budget request settings: deletion direntry, new direntry, removing
1341 * the old inode, and changing old and new parent directory inodes.
1342 *
1343 * However, this operation also marks the target inode as dirty and
1344 * does not write it, so we allocate budget for the target inode
1345 * separately.
1346 */
1347
9e0a1fff 1348 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
4cb2a01d 1349 old_dentry, old_inode->i_ino, old_dir->i_ino,
9e0a1fff
RW
1350 new_dentry, new_dir->i_ino, flags);
1351
82c1593c 1352 if (unlink)
5955102c 1353 ubifs_assert(inode_is_locked(new_inode));
82c1593c 1354
ac7e47a9
RW
1355 if (old_dir != new_dir) {
1356 if (ubifs_crypt_is_encrypted(new_dir) &&
1357 !fscrypt_has_permitted_context(new_dir, old_inode))
1358 return -EPERM;
1359 }
1360
1e51764a 1361 if (unlink && is_dir) {
f6337d84 1362 err = ubifs_check_dir_empty(new_inode);
1e51764a
AB
1363 if (err)
1364 return err;
1365 }
1366
f4f61d2c 1367 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
1e51764a
AB
1368 if (err)
1369 return err;
f4f61d2c
RW
1370
1371 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
1372 if (err) {
1373 fscrypt_free_filename(&old_nm);
1374 return err;
1375 }
1376
1377 new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
1378 old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
1379
1380 err = ubifs_budget_space(c, &req);
1381 if (err) {
1382 fscrypt_free_filename(&old_nm);
1383 fscrypt_free_filename(&new_nm);
1384 return err;
1385 }
1e51764a
AB
1386 err = ubifs_budget_space(c, &ino_req);
1387 if (err) {
f4f61d2c
RW
1388 fscrypt_free_filename(&old_nm);
1389 fscrypt_free_filename(&new_nm);
1e51764a
AB
1390 ubifs_release_budget(c, &req);
1391 return err;
1392 }
1393
9e0a1fff
RW
1394 if (flags & RENAME_WHITEOUT) {
1395 union ubifs_dev_desc *dev = NULL;
1396
1397 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1398 if (!dev) {
1399 ubifs_release_budget(c, &req);
1400 ubifs_release_budget(c, &ino_req);
1401 return -ENOMEM;
1402 }
1403
1404 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1405 if (err) {
1406 ubifs_release_budget(c, &req);
1407 ubifs_release_budget(c, &ino_req);
1408 kfree(dev);
1409 return err;
1410 }
1411
1412 whiteout->i_state |= I_LINKABLE;
1413 whiteout_ui = ubifs_inode(whiteout);
1414 whiteout_ui->data = dev;
1415 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
1416 ubifs_assert(!whiteout_ui->dirty);
1417 }
1418
1419 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1e51764a
AB
1420
1421 /*
1422 * Like most other Unix systems, set the @i_ctime for inodes on a
1423 * rename.
1424 */
607a11ad 1425 time = current_time(old_dir);
1e51764a
AB
1426 old_inode->i_ctime = time;
1427
1428 /* We must adjust parent link count when renaming directories */
1429 if (is_dir) {
1430 if (move) {
1431 /*
1432 * @old_dir loses a link because we are moving
1433 * @old_inode to a different directory.
1434 */
1435 drop_nlink(old_dir);
1436 /*
1437 * @new_dir only gains a link if we are not also
1438 * overwriting an existing directory.
1439 */
1440 if (!unlink)
1441 inc_nlink(new_dir);
1442 } else {
1443 /*
1444 * @old_inode is not moving to a different directory,
1445 * but @old_dir still loses a link if we are
1446 * overwriting an existing directory.
1447 */
1448 if (unlink)
1449 drop_nlink(old_dir);
1450 }
1451 }
1452
1453 old_dir->i_size -= old_sz;
1454 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1455 old_dir->i_mtime = old_dir->i_ctime = time;
1456 new_dir->i_mtime = new_dir->i_ctime = time;
1457
1458 /*
1459 * And finally, if we unlinked a direntry which happened to have the
1460 * same name as the moved direntry, we have to decrement @i_nlink of
1461 * the unlinked inode and change its ctime.
1462 */
1463 if (unlink) {
1464 /*
1465 * Directories cannot have hard-links, so if this is a
c43be108 1466 * directory, just clear @i_nlink.
1e51764a 1467 */
c43be108 1468 saved_nlink = new_inode->i_nlink;
1e51764a 1469 if (is_dir)
c43be108
AB
1470 clear_nlink(new_inode);
1471 else
1e51764a
AB
1472 drop_nlink(new_inode);
1473 new_inode->i_ctime = time;
1e51764a
AB
1474 } else {
1475 new_dir->i_size += new_sz;
1476 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1477 }
1478
1479 /*
1480 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1481 * is dirty, because this will be done later on at the end of
1482 * 'ubifs_rename()'.
1483 */
1484 if (IS_SYNC(old_inode)) {
1485 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1486 if (unlink && IS_SYNC(new_inode))
1487 sync = 1;
1488 }
9e0a1fff
RW
1489
1490 if (whiteout) {
1491 struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1492 .dirtied_ino_d = \
1493 ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1494
1495 err = ubifs_budget_space(c, &wht_req);
1496 if (err) {
1497 ubifs_release_budget(c, &req);
1498 ubifs_release_budget(c, &ino_req);
1499 kfree(whiteout_ui->data);
1500 whiteout_ui->data_len = 0;
1501 iput(whiteout);
1502 return err;
1503 }
1504
1505 inc_nlink(whiteout);
1506 mark_inode_dirty(whiteout);
1507 whiteout->i_state &= ~I_LINKABLE;
1508 iput(whiteout);
1509 }
1510
f4f61d2c
RW
1511 err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
1512 new_inode, &new_nm, whiteout, sync);
1e51764a
AB
1513 if (err)
1514 goto out_cancel;
1515
9e0a1fff 1516 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1e51764a
AB
1517 ubifs_release_budget(c, &req);
1518
1519 mutex_lock(&old_inode_ui->ui_mutex);
1520 release = old_inode_ui->dirty;
1521 mark_inode_dirty_sync(old_inode);
1522 mutex_unlock(&old_inode_ui->ui_mutex);
1523
1524 if (release)
1525 ubifs_release_budget(c, &ino_req);
1526 if (IS_SYNC(old_inode))
a9185b41 1527 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
f4f61d2c
RW
1528
1529 fscrypt_free_filename(&old_nm);
1530 fscrypt_free_filename(&new_nm);
1e51764a
AB
1531 return err;
1532
1533out_cancel:
1534 if (unlink) {
c43be108 1535 set_nlink(new_inode, saved_nlink);
1e51764a
AB
1536 } else {
1537 new_dir->i_size -= new_sz;
1538 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1539 }
1540 old_dir->i_size += old_sz;
1541 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1542 if (is_dir) {
1543 if (move) {
1544 inc_nlink(old_dir);
1545 if (!unlink)
1546 drop_nlink(new_dir);
1547 } else {
1548 if (unlink)
1549 inc_nlink(old_dir);
1550 }
1551 }
9e0a1fff
RW
1552 if (whiteout) {
1553 drop_nlink(whiteout);
1554 iput(whiteout);
1555 }
1556 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1e51764a
AB
1557 ubifs_release_budget(c, &ino_req);
1558 ubifs_release_budget(c, &req);
f4f61d2c
RW
1559 fscrypt_free_filename(&old_nm);
1560 fscrypt_free_filename(&new_nm);
1e51764a
AB
1561 return err;
1562}
1563
9ec64962
RW
1564static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1565 struct inode *new_dir, struct dentry *new_dentry)
1566{
1567 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1568 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1569 .dirtied_ino = 2 };
1570 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1571 struct inode *fst_inode = d_inode(old_dentry);
1572 struct inode *snd_inode = d_inode(new_dentry);
1573 struct timespec time;
1574 int err;
f4f61d2c 1575 struct fscrypt_name fst_nm, snd_nm;
9ec64962
RW
1576
1577 ubifs_assert(fst_inode && snd_inode);
1578
ac7e47a9
RW
1579 if ((ubifs_crypt_is_encrypted(old_dir) ||
1580 ubifs_crypt_is_encrypted(new_dir)) &&
1581 (old_dir != new_dir) &&
1582 (!fscrypt_has_permitted_context(new_dir, fst_inode) ||
1583 !fscrypt_has_permitted_context(old_dir, snd_inode)))
1584 return -EPERM;
1585
f4f61d2c
RW
1586 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
1587 if (err)
1588 return err;
1589
1590 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
1591 if (err) {
1592 fscrypt_free_filename(&fst_nm);
1593 return err;
1594 }
1595
9ec64962
RW
1596 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1597
607a11ad 1598 time = current_time(old_dir);
9ec64962
RW
1599 fst_inode->i_ctime = time;
1600 snd_inode->i_ctime = time;
1601 old_dir->i_mtime = old_dir->i_ctime = time;
1602 new_dir->i_mtime = new_dir->i_ctime = time;
1603
1604 if (old_dir != new_dir) {
1605 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1606 inc_nlink(new_dir);
1607 drop_nlink(old_dir);
1608 }
1609 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1610 drop_nlink(new_dir);
1611 inc_nlink(old_dir);
1612 }
1613 }
1614
f4f61d2c
RW
1615 err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
1616 snd_inode, &snd_nm, sync);
9ec64962
RW
1617
1618 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1619 ubifs_release_budget(c, &req);
1620
f4f61d2c
RW
1621 fscrypt_free_filename(&fst_nm);
1622 fscrypt_free_filename(&snd_nm);
9ec64962
RW
1623 return err;
1624}
1625
390975ac 1626static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
9ec64962
RW
1627 struct inode *new_dir, struct dentry *new_dentry,
1628 unsigned int flags)
1629{
1630 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1631 return -EINVAL;
1632
1633 ubifs_assert(inode_is_locked(old_dir));
1634 ubifs_assert(inode_is_locked(new_dir));
1635
1636 if (flags & RENAME_EXCHANGE)
1637 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1638
390975ac 1639 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
9ec64962
RW
1640}
1641
a528d35e
DH
1642int ubifs_getattr(const struct path *path, struct kstat *stat,
1643 u32 request_mask, unsigned int flags)
1e51764a
AB
1644{
1645 loff_t size;
a528d35e 1646 struct inode *inode = d_inode(path->dentry);
1e51764a
AB
1647 struct ubifs_inode *ui = ubifs_inode(inode);
1648
1649 mutex_lock(&ui->ui_mutex);
6d42e7e9 1650 generic_fillattr(inode, stat);
1e51764a
AB
1651 stat->blksize = UBIFS_BLOCK_SIZE;
1652 stat->size = ui->ui_size;
1653
1654 /*
1655 * Unfortunately, the 'stat()' system call was designed for block
1656 * device based file systems, and it is not appropriate for UBIFS,
1657 * because UBIFS does not have notion of "block". For example, it is
1658 * difficult to tell how many block a directory takes - it actually
1659 * takes less than 300 bytes, but we have to round it to block size,
1660 * which introduces large mistake. This makes utilities like 'du' to
1661 * report completely senseless numbers. This is the reason why UBIFS
1662 * goes the same way as JFFS2 - it reports zero blocks for everything
1663 * but regular files, which makes more sense than reporting completely
1664 * wrong sizes.
1665 */
1666 if (S_ISREG(inode->i_mode)) {
1667 size = ui->xattr_size;
1668 size += stat->size;
1669 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1670 /*
1671 * Note, user-space expects 512-byte blocks count irrespectively
1672 * of what was reported in @stat->size.
1673 */
1674 stat->blocks = size >> 9;
1675 } else
1676 stat->blocks = 0;
1677 mutex_unlock(&ui->ui_mutex);
1678 return 0;
1679}
1680
ba40e6a3
RW
1681static int ubifs_dir_open(struct inode *dir, struct file *file)
1682{
1683 if (ubifs_crypt_is_encrypted(dir))
1684 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1685
1686 return 0;
1687}
1688
e8b81566 1689const struct inode_operations ubifs_dir_inode_operations = {
1e51764a
AB
1690 .lookup = ubifs_lookup,
1691 .create = ubifs_create,
1692 .link = ubifs_link,
1693 .symlink = ubifs_symlink,
1694 .unlink = ubifs_unlink,
1695 .mkdir = ubifs_mkdir,
1696 .rmdir = ubifs_rmdir,
1697 .mknod = ubifs_mknod,
390975ac 1698 .rename = ubifs_rename,
1e51764a
AB
1699 .setattr = ubifs_setattr,
1700 .getattr = ubifs_getattr,
1e51764a 1701 .listxattr = ubifs_listxattr,
8c1c5f26
DY
1702#ifdef CONFIG_UBIFS_ATIME_SUPPORT
1703 .update_time = ubifs_update_time,
1704#endif
474b9370 1705 .tmpfile = ubifs_tmpfile,
1e51764a
AB
1706};
1707
e8b81566 1708const struct file_operations ubifs_dir_operations = {
01122e06 1709 .llseek = generic_file_llseek,
1e51764a
AB
1710 .release = ubifs_dir_release,
1711 .read = generic_read_dir,
c51da20c 1712 .iterate_shared = ubifs_readdir,
1e51764a
AB
1713 .fsync = ubifs_fsync,
1714 .unlocked_ioctl = ubifs_ioctl,
ba40e6a3 1715 .open = ubifs_dir_open,
1e51764a
AB
1716#ifdef CONFIG_COMPAT
1717 .compat_ioctl = ubifs_compat_ioctl,
1718#endif
1719};