]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ubifs/dir.c
ubifs: Fix RENAME_WHITEOUT support
[mirror_ubuntu-artful-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
AB
123 inode->i_mtime = inode->i_atime = inode->i_ctime =
124 ubifs_current_time(inode);
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
288done:
289 kfree(dent);
f4f61d2c 290 fscrypt_free_filename(&nm);
1e51764a
AB
291 /*
292 * Note, d_splice_alias() would be required instead if we supported
293 * NFS.
294 */
295 d_add(dentry, inode);
296 return NULL;
297
f4f61d2c 298out_dent:
1e51764a 299 kfree(dent);
f4f61d2c
RW
300out_fname:
301 fscrypt_free_filename(&nm);
1e51764a
AB
302 return ERR_PTR(err);
303}
304
4acdaf27 305static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 306 bool excl)
1e51764a
AB
307{
308 struct inode *inode;
309 struct ubifs_info *c = dir->i_sb->s_fs_info;
1e51764a
AB
310 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
311 .dirtied_ino = 1 };
312 struct ubifs_inode *dir_ui = ubifs_inode(dir);
f4f61d2c
RW
313 struct fscrypt_name nm;
314 int err, sz_change;
1e51764a
AB
315
316 /*
317 * Budget request settings: new inode, new direntry, changing the
318 * parent directory inode.
319 */
320
4cb2a01d
AV
321 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
322 dentry, mode, dir->i_ino);
1e51764a
AB
323
324 err = ubifs_budget_space(c, &req);
325 if (err)
326 return err;
327
f4f61d2c
RW
328 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
1e51764a
AB
334 inode = ubifs_new_inode(c, dir, mode);
335 if (IS_ERR(inode)) {
336 err = PTR_ERR(inode);
f4f61d2c 337 goto out_fname;
1e51764a
AB
338 }
339
d7f0b70d
SN
340 err = ubifs_init_security(dir, inode, &dentry->d_name);
341 if (err)
9401a795 342 goto out_inode;
d7f0b70d 343
1e51764a
AB
344 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;
f4f61d2c 348 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a
AB
349 if (err)
350 goto out_cancel;
351 mutex_unlock(&dir_ui->ui_mutex);
352
353 ubifs_release_budget(c, &req);
f4f61d2c 354 fscrypt_free_filename(&nm);
1e51764a
AB
355 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);
9401a795 363out_inode:
1e51764a
AB
364 make_bad_inode(inode);
365 iput(inode);
f4f61d2c
RW
366out_fname:
367 fscrypt_free_filename(&nm);
1e51764a
AB
368out_budg:
369 ubifs_release_budget(c, &req);
235c362b 370 ubifs_err(c, "cannot create regular file, error %d", err);
1e51764a
AB
371 return err;
372}
373
9e0a1fff
RW
374static int do_tmpfile(struct inode *dir, struct dentry *dentry,
375 umode_t mode, struct inode **whiteout)
474b9370
RW
376{
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;
f4f61d2c 383 struct fscrypt_name nm;
474b9370
RW
384
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
f4f61d2c 393 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
474b9370
RW
394 if (err)
395 return err;
396
f4f61d2c
RW
397 err = ubifs_budget_space(c, &req);
398 if (err) {
399 fscrypt_free_filename(&nm);
400 return err;
401 }
402
474b9370
RW
403 err = ubifs_budget_space(c, &ino_req);
404 if (err) {
405 ubifs_release_budget(c, &req);
f4f61d2c 406 fscrypt_free_filename(&nm);
474b9370
RW
407 return err;
408 }
409
410 inode = ubifs_new_inode(c, dir, mode);
411 if (IS_ERR(inode)) {
412 err = PTR_ERR(inode);
413 goto out_budg;
414 }
415 ui = ubifs_inode(inode);
416
9e0a1fff
RW
417 if (whiteout) {
418 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
419 ubifs_assert(inode->i_op == &ubifs_file_inode_operations);
420 }
421
474b9370
RW
422 err = ubifs_init_security(dir, inode, &dentry->d_name);
423 if (err)
424 goto out_inode;
425
426 mutex_lock(&ui->ui_mutex);
427 insert_inode_hash(inode);
9e0a1fff
RW
428
429 if (whiteout) {
430 mark_inode_dirty(inode);
431 drop_nlink(inode);
432 *whiteout = inode;
433 } else {
434 d_tmpfile(dentry, inode);
435 }
474b9370 436 ubifs_assert(ui->dirty);
9e0a1fff 437
474b9370
RW
438 instantiated = 1;
439 mutex_unlock(&ui->ui_mutex);
440
441 mutex_lock(&dir_ui->ui_mutex);
f4f61d2c 442 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
474b9370
RW
443 if (err)
444 goto out_cancel;
445 mutex_unlock(&dir_ui->ui_mutex);
446
447 ubifs_release_budget(c, &req);
448
449 return 0;
450
451out_cancel:
452 mutex_unlock(&dir_ui->ui_mutex);
453out_inode:
454 make_bad_inode(inode);
455 if (!instantiated)
456 iput(inode);
457out_budg:
458 ubifs_release_budget(c, &req);
459 if (!instantiated)
460 ubifs_release_budget(c, &ino_req);
f4f61d2c 461 fscrypt_free_filename(&nm);
474b9370
RW
462 ubifs_err(c, "cannot create temporary file, error %d", err);
463 return err;
464}
465
9e0a1fff
RW
466static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
467 umode_t mode)
468{
469 return do_tmpfile(dir, dentry, mode, NULL);
470}
471
1e51764a
AB
472/**
473 * vfs_dent_type - get VFS directory entry type.
474 * @type: UBIFS directory entry type
475 *
476 * This function converts UBIFS directory entry type into VFS directory entry
477 * type.
478 */
479static unsigned int vfs_dent_type(uint8_t type)
480{
481 switch (type) {
482 case UBIFS_ITYPE_REG:
483 return DT_REG;
484 case UBIFS_ITYPE_DIR:
485 return DT_DIR;
486 case UBIFS_ITYPE_LNK:
487 return DT_LNK;
488 case UBIFS_ITYPE_BLK:
489 return DT_BLK;
490 case UBIFS_ITYPE_CHR:
491 return DT_CHR;
492 case UBIFS_ITYPE_FIFO:
493 return DT_FIFO;
494 case UBIFS_ITYPE_SOCK:
495 return DT_SOCK;
496 default:
497 BUG();
498 }
499 return 0;
500}
501
502/*
503 * The classical Unix view for directory is that it is a linear array of
504 * (name, inode number) entries. Linux/VFS assumes this model as well.
505 * Particularly, 'readdir()' call wants us to return a directory entry offset
506 * which later may be used to continue 'readdir()'ing the directory or to
507 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
508 * model because directory entries are identified by keys, which may collide.
509 *
510 * UBIFS uses directory entry hash value for directory offsets, so
511 * 'seekdir()'/'telldir()' may not always work because of possible key
512 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
513 * properly by means of saving full directory entry name in the private field
514 * of the file description object.
515 *
516 * This means that UBIFS cannot support NFS which requires full
517 * 'seekdir()'/'telldir()' support.
518 */
01122e06 519static int ubifs_readdir(struct file *file, struct dir_context *ctx)
1e51764a 520{
ba75d570 521 int fstr_real_len = 0, err = 0;
f4f61d2c
RW
522 struct fscrypt_name nm;
523 struct fscrypt_str fstr = {0};
1e51764a
AB
524 union ubifs_key key;
525 struct ubifs_dent_node *dent;
496ad9aa 526 struct inode *dir = file_inode(file);
1e51764a 527 struct ubifs_info *c = dir->i_sb->s_fs_info;
f4f61d2c 528 bool encrypted = ubifs_crypt_is_encrypted(dir);
1e51764a 529
01122e06 530 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
1e51764a 531
01122e06 532 if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
1e51764a
AB
533 /*
534 * The directory was seek'ed to a senseless position or there
535 * are no more entries.
536 */
537 return 0;
538
f4f61d2c
RW
539 if (encrypted) {
540 err = fscrypt_get_encryption_info(dir);
541 if (err && err != -ENOKEY)
542 return err;
543
544 err = fscrypt_fname_alloc_buffer(dir, UBIFS_MAX_NLEN, &fstr);
545 if (err)
546 return err;
547
548 fstr_real_len = fstr.len;
549 }
550
605c912b
AB
551 if (file->f_version == 0) {
552 /*
553 * The file was seek'ed, which means that @file->private_data
554 * is now invalid. This may also be just the first
555 * 'ubifs_readdir()' invocation, in which case
556 * @file->private_data is NULL, and the below code is
557 * basically a no-op.
558 */
559 kfree(file->private_data);
560 file->private_data = NULL;
561 }
562
563 /*
564 * 'generic_file_llseek()' unconditionally sets @file->f_version to
565 * zero, and we use this for detecting whether the file was seek'ed.
566 */
567 file->f_version = 1;
568
1e51764a 569 /* File positions 0 and 1 correspond to "." and ".." */
01122e06 570 if (ctx->pos < 2) {
1e51764a 571 ubifs_assert(!file->private_data);
f4f61d2c
RW
572 if (!dir_emit_dots(file, ctx)) {
573 if (encrypted)
574 fscrypt_fname_free_buffer(&fstr);
1e51764a 575 return 0;
f4f61d2c 576 }
1e51764a
AB
577
578 /* Find the first entry in TNC and save it */
579 lowest_dent_key(c, &key, dir->i_ino);
f4f61d2c 580 fname_len(&nm) = 0;
1e51764a
AB
581 dent = ubifs_tnc_next_ent(c, &key, &nm);
582 if (IS_ERR(dent)) {
583 err = PTR_ERR(dent);
584 goto out;
585 }
586
01122e06 587 ctx->pos = key_hash_flash(c, &dent->key);
1e51764a
AB
588 file->private_data = dent;
589 }
590
591 dent = file->private_data;
592 if (!dent) {
593 /*
594 * The directory was seek'ed to and is now readdir'ed.
01122e06 595 * Find the entry corresponding to @ctx->pos or the closest one.
1e51764a 596 */
01122e06 597 dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
f4f61d2c 598 fname_len(&nm) = 0;
1e51764a
AB
599 dent = ubifs_tnc_next_ent(c, &key, &nm);
600 if (IS_ERR(dent)) {
601 err = PTR_ERR(dent);
602 goto out;
603 }
01122e06 604 ctx->pos = key_hash_flash(c, &dent->key);
1e51764a
AB
605 file->private_data = dent;
606 }
607
608 while (1) {
b20e2d99
HL
609 dbg_gen("ino %llu, new f_pos %#x",
610 (unsigned long long)le64_to_cpu(dent->inum),
1e51764a 611 key_hash_flash(c, &dent->key));
0ecb9529
HH
612 ubifs_assert(le64_to_cpu(dent->ch.sqnum) >
613 ubifs_inode(dir)->creat_sqnum);
1e51764a 614
f4f61d2c
RW
615 fname_len(&nm) = le16_to_cpu(dent->nlen);
616 fname_name(&nm) = dent->name;
617
618 if (encrypted) {
619 fstr.len = fstr_real_len;
620
528e3d17
RW
621 err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
622 &dent->key),
623 le32_to_cpu(dent->cookie),
624 &nm.disk_name, &fstr);
ca7f85be 625 if (err)
f4f61d2c
RW
626 goto out;
627 } else {
628 fstr.len = fname_len(&nm);
629 fstr.name = fname_name(&nm);
630 }
631
632 if (!dir_emit(ctx, fstr.name, fstr.len,
1e51764a 633 le64_to_cpu(dent->inum),
f4f61d2c
RW
634 vfs_dent_type(dent->type))) {
635 if (encrypted)
636 fscrypt_fname_free_buffer(&fstr);
1e51764a 637 return 0;
f4f61d2c 638 }
1e51764a
AB
639
640 /* Switch to the next entry */
641 key_read(c, &dent->key, &key);
1e51764a
AB
642 dent = ubifs_tnc_next_ent(c, &key, &nm);
643 if (IS_ERR(dent)) {
644 err = PTR_ERR(dent);
645 goto out;
646 }
647
648 kfree(file->private_data);
01122e06 649 ctx->pos = key_hash_flash(c, &dent->key);
1e51764a
AB
650 file->private_data = dent;
651 cond_resched();
652 }
653
654out:
aeeb14f7
RW
655 kfree(file->private_data);
656 file->private_data = NULL;
657
f4f61d2c
RW
658 if (encrypted)
659 fscrypt_fname_free_buffer(&fstr);
660
c83ed4c9 661 if (err != -ENOENT)
235c362b 662 ubifs_err(c, "cannot find next direntry, error %d", err);
a00052a2
RW
663 else
664 /*
665 * -ENOENT is a non-fatal error in this context, the TNC uses
666 * it to indicate that the cursor moved past the current directory
667 * and readdir() has to stop.
668 */
669 err = 0;
670
1e51764a 671
605c912b 672 /* 2 is a special value indicating that there are no more direntries */
01122e06 673 ctx->pos = 2;
c83ed4c9 674 return err;
1e51764a
AB
675}
676
1e51764a
AB
677/* Free saved readdir() state when the directory is closed */
678static int ubifs_dir_release(struct inode *dir, struct file *file)
679{
680 kfree(file->private_data);
681 file->private_data = NULL;
682 return 0;
683}
684
685/**
82c1593c 686 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
1e51764a
AB
687 * @inode1: first inode
688 * @inode2: second inode
82c1593c
AB
689 *
690 * We do not implement any tricks to guarantee strict lock ordering, because
691 * VFS has already done it for us on the @i_mutex. So this is just a simple
692 * wrapper function.
1e51764a
AB
693 */
694static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
695{
82c1593c
AB
696 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
697 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1e51764a
AB
698}
699
700/**
82c1593c 701 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
1e51764a
AB
702 * @inode1: first inode
703 * @inode2: second inode
704 */
705static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
706{
1e51764a 707 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
82c1593c 708 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1e51764a
AB
709}
710
711static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
712 struct dentry *dentry)
713{
714 struct ubifs_info *c = dir->i_sb->s_fs_info;
2b0143b5 715 struct inode *inode = d_inode(old_dentry);
1e51764a
AB
716 struct ubifs_inode *ui = ubifs_inode(inode);
717 struct ubifs_inode *dir_ui = ubifs_inode(dir);
718 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
719 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
dab4b4d2 720 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
f4f61d2c 721 struct fscrypt_name nm;
1e51764a
AB
722
723 /*
724 * Budget request settings: new direntry, changing the target inode,
725 * changing the parent inode.
726 */
727
4cb2a01d
AV
728 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
729 dentry, inode->i_ino,
1e51764a 730 inode->i_nlink, dir->i_ino);
5955102c
AV
731 ubifs_assert(inode_is_locked(dir));
732 ubifs_assert(inode_is_locked(inode));
8b3884a8 733
3d4b2fcb
EB
734 if (ubifs_crypt_is_encrypted(dir) &&
735 !fscrypt_has_permitted_context(dir, inode))
736 return -EPERM;
f4f61d2c
RW
737
738 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1e51764a
AB
739 if (err)
740 return err;
741
f4f61d2c
RW
742 err = dbg_check_synced_i_size(c, inode);
743 if (err)
744 goto out_fname;
745
1e51764a
AB
746 err = ubifs_budget_space(c, &req);
747 if (err)
f4f61d2c 748 goto out_fname;
1e51764a
AB
749
750 lock_2_inodes(dir, inode);
751 inc_nlink(inode);
7de9c6ee 752 ihold(inode);
1e51764a
AB
753 inode->i_ctime = ubifs_current_time(inode);
754 dir->i_size += sz_change;
755 dir_ui->ui_size = dir->i_size;
756 dir->i_mtime = dir->i_ctime = inode->i_ctime;
f4f61d2c 757 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a
AB
758 if (err)
759 goto out_cancel;
760 unlock_2_inodes(dir, inode);
761
762 ubifs_release_budget(c, &req);
763 d_instantiate(dentry, inode);
f4f61d2c 764 fscrypt_free_filename(&nm);
1e51764a
AB
765 return 0;
766
767out_cancel:
768 dir->i_size -= sz_change;
769 dir_ui->ui_size = dir->i_size;
770 drop_nlink(inode);
771 unlock_2_inodes(dir, inode);
772 ubifs_release_budget(c, &req);
773 iput(inode);
f4f61d2c
RW
774out_fname:
775 fscrypt_free_filename(&nm);
1e51764a
AB
776 return err;
777}
778
779static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
780{
781 struct ubifs_info *c = dir->i_sb->s_fs_info;
2b0143b5 782 struct inode *inode = d_inode(dentry);
1e51764a 783 struct ubifs_inode *dir_ui = ubifs_inode(dir);
f4f61d2c 784 int err, sz_change, budgeted = 1;
1e51764a 785 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
c43be108 786 unsigned int saved_nlink = inode->i_nlink;
f4f61d2c 787 struct fscrypt_name nm;
1e51764a
AB
788
789 /*
790 * Budget request settings: deletion direntry, deletion inode (+1 for
791 * @dirtied_ino), changing the parent directory inode. If budgeting
792 * fails, go ahead anyway because we have extra space reserved for
793 * deletions.
794 */
795
4cb2a01d
AV
796 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
797 dentry, inode->i_ino,
1e51764a 798 inode->i_nlink, dir->i_ino);
f4f61d2c
RW
799
800 if (ubifs_crypt_is_encrypted(dir)) {
801 err = fscrypt_get_encryption_info(dir);
802 if (err && err != -ENOKEY)
803 return err;
804 }
805
806 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
807 if (err)
808 return err;
809
810 sz_change = CALC_DENT_SIZE(fname_len(&nm));
811
5955102c
AV
812 ubifs_assert(inode_is_locked(dir));
813 ubifs_assert(inode_is_locked(inode));
d808efb4 814 err = dbg_check_synced_i_size(c, inode);
1e51764a 815 if (err)
f4f61d2c 816 goto out_fname;
1e51764a
AB
817
818 err = ubifs_budget_space(c, &req);
819 if (err) {
820 if (err != -ENOSPC)
f4f61d2c 821 goto out_fname;
1e51764a
AB
822 budgeted = 0;
823 }
824
825 lock_2_inodes(dir, inode);
826 inode->i_ctime = ubifs_current_time(dir);
827 drop_nlink(inode);
828 dir->i_size -= sz_change;
829 dir_ui->ui_size = dir->i_size;
830 dir->i_mtime = dir->i_ctime = inode->i_ctime;
f4f61d2c 831 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
1e51764a
AB
832 if (err)
833 goto out_cancel;
834 unlock_2_inodes(dir, inode);
835
836 if (budgeted)
837 ubifs_release_budget(c, &req);
838 else {
839 /* We've deleted something - clean the "no space" flags */
b137545c 840 c->bi.nospace = c->bi.nospace_rp = 0;
1e51764a
AB
841 smp_wmb();
842 }
f4f61d2c 843 fscrypt_free_filename(&nm);
1e51764a
AB
844 return 0;
845
846out_cancel:
847 dir->i_size += sz_change;
848 dir_ui->ui_size = dir->i_size;
c43be108 849 set_nlink(inode, saved_nlink);
1e51764a
AB
850 unlock_2_inodes(dir, inode);
851 if (budgeted)
852 ubifs_release_budget(c, &req);
f4f61d2c
RW
853out_fname:
854 fscrypt_free_filename(&nm);
1e51764a
AB
855 return err;
856}
857
858/**
859 * check_dir_empty - check if a directory is empty or not.
1e51764a
AB
860 * @dir: VFS inode object of the directory to check
861 *
862 * This function checks if directory @dir is empty. Returns zero if the
863 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
864 * in case of of errors.
865 */
f6337d84 866int ubifs_check_dir_empty(struct inode *dir)
1e51764a 867{
f6337d84 868 struct ubifs_info *c = dir->i_sb->s_fs_info;
f4f61d2c 869 struct fscrypt_name nm = { 0 };
1e51764a
AB
870 struct ubifs_dent_node *dent;
871 union ubifs_key key;
872 int err;
873
874 lowest_dent_key(c, &key, dir->i_ino);
875 dent = ubifs_tnc_next_ent(c, &key, &nm);
876 if (IS_ERR(dent)) {
877 err = PTR_ERR(dent);
878 if (err == -ENOENT)
879 err = 0;
880 } else {
881 kfree(dent);
882 err = -ENOTEMPTY;
883 }
884 return err;
885}
886
887static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
888{
889 struct ubifs_info *c = dir->i_sb->s_fs_info;
2b0143b5 890 struct inode *inode = d_inode(dentry);
f4f61d2c 891 int err, sz_change, budgeted = 1;
1e51764a
AB
892 struct ubifs_inode *dir_ui = ubifs_inode(dir);
893 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
f4f61d2c 894 struct fscrypt_name nm;
1e51764a
AB
895
896 /*
897 * Budget request settings: deletion direntry, deletion inode and
898 * changing the parent inode. If budgeting fails, go ahead anyway
899 * because we have extra space reserved for deletions.
900 */
901
4cb2a01d
AV
902 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
903 inode->i_ino, dir->i_ino);
5955102c
AV
904 ubifs_assert(inode_is_locked(dir));
905 ubifs_assert(inode_is_locked(inode));
f6337d84 906 err = ubifs_check_dir_empty(d_inode(dentry));
1e51764a
AB
907 if (err)
908 return err;
909
f4f61d2c
RW
910 if (ubifs_crypt_is_encrypted(dir)) {
911 err = fscrypt_get_encryption_info(dir);
912 if (err && err != -ENOKEY)
913 return err;
914 }
915
916 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
917 if (err)
918 return err;
919
920 sz_change = CALC_DENT_SIZE(fname_len(&nm));
921
1e51764a
AB
922 err = ubifs_budget_space(c, &req);
923 if (err) {
924 if (err != -ENOSPC)
f4f61d2c 925 goto out_fname;
1e51764a
AB
926 budgeted = 0;
927 }
928
929 lock_2_inodes(dir, inode);
930 inode->i_ctime = ubifs_current_time(dir);
931 clear_nlink(inode);
932 drop_nlink(dir);
933 dir->i_size -= sz_change;
934 dir_ui->ui_size = dir->i_size;
935 dir->i_mtime = dir->i_ctime = inode->i_ctime;
f4f61d2c 936 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
1e51764a
AB
937 if (err)
938 goto out_cancel;
939 unlock_2_inodes(dir, inode);
940
941 if (budgeted)
942 ubifs_release_budget(c, &req);
943 else {
944 /* We've deleted something - clean the "no space" flags */
b137545c 945 c->bi.nospace = c->bi.nospace_rp = 0;
1e51764a
AB
946 smp_wmb();
947 }
f4f61d2c 948 fscrypt_free_filename(&nm);
1e51764a
AB
949 return 0;
950
951out_cancel:
952 dir->i_size += sz_change;
953 dir_ui->ui_size = dir->i_size;
954 inc_nlink(dir);
c43be108 955 set_nlink(inode, 2);
1e51764a
AB
956 unlock_2_inodes(dir, inode);
957 if (budgeted)
958 ubifs_release_budget(c, &req);
f4f61d2c
RW
959out_fname:
960 fscrypt_free_filename(&nm);
1e51764a
AB
961 return err;
962}
963
18bb1db3 964static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1e51764a
AB
965{
966 struct inode *inode;
967 struct ubifs_inode *dir_ui = ubifs_inode(dir);
968 struct ubifs_info *c = dir->i_sb->s_fs_info;
f4f61d2c 969 int err, sz_change;
182854b4 970 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 };
f4f61d2c 971 struct fscrypt_name nm;
1e51764a
AB
972
973 /*
974 * Budget request settings: new inode, new direntry and changing parent
975 * directory inode.
976 */
977
4cb2a01d
AV
978 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
979 dentry, mode, dir->i_ino);
1e51764a
AB
980
981 err = ubifs_budget_space(c, &req);
982 if (err)
983 return err;
984
f4f61d2c
RW
985 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
986 if (err)
987 goto out_budg;
988
989 sz_change = CALC_DENT_SIZE(fname_len(&nm));
990
1e51764a
AB
991 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
992 if (IS_ERR(inode)) {
993 err = PTR_ERR(inode);
f4f61d2c 994 goto out_fname;
1e51764a
AB
995 }
996
d7f0b70d
SN
997 err = ubifs_init_security(dir, inode, &dentry->d_name);
998 if (err)
9401a795 999 goto out_inode;
d7f0b70d 1000
1e51764a
AB
1001 mutex_lock(&dir_ui->ui_mutex);
1002 insert_inode_hash(inode);
1003 inc_nlink(inode);
1004 inc_nlink(dir);
1005 dir->i_size += sz_change;
1006 dir_ui->ui_size = dir->i_size;
1007 dir->i_mtime = dir->i_ctime = inode->i_ctime;
f4f61d2c 1008 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a 1009 if (err) {
235c362b 1010 ubifs_err(c, "cannot create directory, error %d", err);
1e51764a
AB
1011 goto out_cancel;
1012 }
1013 mutex_unlock(&dir_ui->ui_mutex);
1014
1015 ubifs_release_budget(c, &req);
1016 d_instantiate(dentry, inode);
f4f61d2c 1017 fscrypt_free_filename(&nm);
1e51764a
AB
1018 return 0;
1019
1020out_cancel:
1021 dir->i_size -= sz_change;
1022 dir_ui->ui_size = dir->i_size;
1023 drop_nlink(dir);
1024 mutex_unlock(&dir_ui->ui_mutex);
9401a795 1025out_inode:
1e51764a
AB
1026 make_bad_inode(inode);
1027 iput(inode);
f4f61d2c
RW
1028out_fname:
1029 fscrypt_free_filename(&nm);
1e51764a
AB
1030out_budg:
1031 ubifs_release_budget(c, &req);
1032 return err;
1033}
1034
1035static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
1a67aafb 1036 umode_t mode, dev_t rdev)
1e51764a
AB
1037{
1038 struct inode *inode;
1039 struct ubifs_inode *ui;
1040 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1041 struct ubifs_info *c = dir->i_sb->s_fs_info;
1042 union ubifs_dev_desc *dev = NULL;
f4f61d2c 1043 int sz_change;
1e51764a
AB
1044 int err, devlen = 0;
1045 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
dab4b4d2
AB
1046 .new_ino_d = ALIGN(devlen, 8),
1047 .dirtied_ino = 1 };
f4f61d2c 1048 struct fscrypt_name nm;
1e51764a
AB
1049
1050 /*
1051 * Budget request settings: new inode, new direntry and changing parent
1052 * directory inode.
1053 */
1054
4cb2a01d 1055 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
1e51764a 1056
1e51764a
AB
1057 if (S_ISBLK(mode) || S_ISCHR(mode)) {
1058 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1059 if (!dev)
1060 return -ENOMEM;
1061 devlen = ubifs_encode_dev(dev, rdev);
1062 }
1063
1064 err = ubifs_budget_space(c, &req);
1065 if (err) {
1066 kfree(dev);
1067 return err;
1068 }
1069
f4f61d2c 1070 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
63ed6573
RW
1071 if (err) {
1072 kfree(dev);
f4f61d2c 1073 goto out_budg;
63ed6573 1074 }
f4f61d2c
RW
1075
1076 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1077
1e51764a
AB
1078 inode = ubifs_new_inode(c, dir, mode);
1079 if (IS_ERR(inode)) {
1080 kfree(dev);
1081 err = PTR_ERR(inode);
f4f61d2c 1082 goto out_fname;
1e51764a
AB
1083 }
1084
1085 init_special_inode(inode, inode->i_mode, rdev);
1086 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
1087 ui = ubifs_inode(inode);
1088 ui->data = dev;
1089 ui->data_len = devlen;
1090
d7f0b70d
SN
1091 err = ubifs_init_security(dir, inode, &dentry->d_name);
1092 if (err)
9401a795 1093 goto out_inode;
d7f0b70d 1094
1e51764a
AB
1095 mutex_lock(&dir_ui->ui_mutex);
1096 dir->i_size += sz_change;
1097 dir_ui->ui_size = dir->i_size;
1098 dir->i_mtime = dir->i_ctime = inode->i_ctime;
f4f61d2c 1099 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a
AB
1100 if (err)
1101 goto out_cancel;
1102 mutex_unlock(&dir_ui->ui_mutex);
1103
1104 ubifs_release_budget(c, &req);
1105 insert_inode_hash(inode);
1106 d_instantiate(dentry, inode);
f4f61d2c 1107 fscrypt_free_filename(&nm);
1e51764a
AB
1108 return 0;
1109
1110out_cancel:
1111 dir->i_size -= sz_change;
1112 dir_ui->ui_size = dir->i_size;
1113 mutex_unlock(&dir_ui->ui_mutex);
9401a795 1114out_inode:
1e51764a
AB
1115 make_bad_inode(inode);
1116 iput(inode);
f4f61d2c
RW
1117out_fname:
1118 fscrypt_free_filename(&nm);
1e51764a
AB
1119out_budg:
1120 ubifs_release_budget(c, &req);
1121 return err;
1122}
1123
1124static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
1125 const char *symname)
1126{
1127 struct inode *inode;
1128 struct ubifs_inode *ui;
1129 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1130 struct ubifs_info *c = dir->i_sb->s_fs_info;
1131 int err, len = strlen(symname);
ca7f85be
RW
1132 int sz_change = CALC_DENT_SIZE(len);
1133 struct fscrypt_str disk_link = FSTR_INIT((char *)symname, len + 1);
1134 struct fscrypt_symlink_data *sd = NULL;
1e51764a 1135 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
dab4b4d2
AB
1136 .new_ino_d = ALIGN(len, 8),
1137 .dirtied_ino = 1 };
ca7f85be
RW
1138 struct fscrypt_name nm;
1139
1140 if (ubifs_crypt_is_encrypted(dir)) {
1141 err = fscrypt_get_encryption_info(dir);
1142 if (err)
1143 goto out_budg;
1144
1145 if (!fscrypt_has_encryption_key(dir)) {
1146 err = -EPERM;
1147 goto out_budg;
1148 }
1149
1150 disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
1151 sizeof(struct fscrypt_symlink_data));
1152 }
1e51764a
AB
1153
1154 /*
1155 * Budget request settings: new inode, new direntry and changing parent
1156 * directory inode.
1157 */
1158
4cb2a01d
AV
1159 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
1160 symname, dir->i_ino);
1e51764a 1161
ca7f85be 1162 if (disk_link.len > UBIFS_MAX_INO_DATA)
1e51764a
AB
1163 return -ENAMETOOLONG;
1164
1165 err = ubifs_budget_space(c, &req);
1166 if (err)
1167 return err;
1168
ca7f85be
RW
1169 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
1170 if (err)
1171 goto out_budg;
1172
1e51764a
AB
1173 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
1174 if (IS_ERR(inode)) {
1175 err = PTR_ERR(inode);
ca7f85be 1176 goto out_fname;
1e51764a
AB
1177 }
1178
1179 ui = ubifs_inode(inode);
ca7f85be 1180 ui->data = kmalloc(disk_link.len, GFP_NOFS);
1e51764a
AB
1181 if (!ui->data) {
1182 err = -ENOMEM;
1183 goto out_inode;
1184 }
1185
ca7f85be
RW
1186 if (ubifs_crypt_is_encrypted(dir)) {
1187 struct qstr istr = QSTR_INIT(symname, len);
1188 struct fscrypt_str ostr;
1189
1190 sd = kzalloc(disk_link.len, GFP_NOFS);
1191 if (!sd) {
1192 err = -ENOMEM;
1193 goto out_inode;
1194 }
1195
ca7f85be
RW
1196 ostr.name = sd->encrypted_path;
1197 ostr.len = disk_link.len;
1198
1199 err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
1200 if (err) {
1201 kfree(sd);
1202 goto out_inode;
1203 }
1204
1205 sd->len = cpu_to_le16(ostr.len);
1206 disk_link.name = (char *)sd;
1207 } else {
1208 inode->i_link = ui->data;
1209 }
1210
1211 memcpy(ui->data, disk_link.name, disk_link.len);
1212 ((char *)ui->data)[disk_link.len - 1] = '\0';
1213
1e51764a
AB
1214 /*
1215 * The terminating zero byte is not written to the flash media and it
1216 * is put just to make later in-memory string processing simpler. Thus,
1217 * data length is @len, not @len + %1.
1218 */
ca7f85be
RW
1219 ui->data_len = disk_link.len - 1;
1220 inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
1e51764a 1221
d7f0b70d
SN
1222 err = ubifs_init_security(dir, inode, &dentry->d_name);
1223 if (err)
9401a795 1224 goto out_inode;
d7f0b70d 1225
1e51764a
AB
1226 mutex_lock(&dir_ui->ui_mutex);
1227 dir->i_size += sz_change;
1228 dir_ui->ui_size = dir->i_size;
1229 dir->i_mtime = dir->i_ctime = inode->i_ctime;
ca7f85be 1230 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1e51764a
AB
1231 if (err)
1232 goto out_cancel;
1233 mutex_unlock(&dir_ui->ui_mutex);
1234
1235 ubifs_release_budget(c, &req);
1236 insert_inode_hash(inode);
1237 d_instantiate(dentry, inode);
ca7f85be 1238 fscrypt_free_filename(&nm);
1e51764a
AB
1239 return 0;
1240
1241out_cancel:
1242 dir->i_size -= sz_change;
1243 dir_ui->ui_size = dir->i_size;
1244 mutex_unlock(&dir_ui->ui_mutex);
1245out_inode:
1246 make_bad_inode(inode);
1247 iput(inode);
ca7f85be
RW
1248out_fname:
1249 fscrypt_free_filename(&nm);
1e51764a
AB
1250out_budg:
1251 ubifs_release_budget(c, &req);
1252 return err;
1253}
1254
1255/**
9e0a1fff 1256 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1e51764a
AB
1257 * @inode1: first inode
1258 * @inode2: second inode
1259 * @inode3: third inode
9e0a1fff 1260 * @inode4: fouth inode
1e51764a 1261 *
82c1593c 1262 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
9e0a1fff 1263 * @inode2 whereas @inode3 and @inode4 may be %NULL.
82c1593c
AB
1264 *
1265 * We do not implement any tricks to guarantee strict lock ordering, because
1266 * VFS has already done it for us on the @i_mutex. So this is just a simple
1267 * wrapper function.
1e51764a 1268 */
9e0a1fff
RW
1269static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1270 struct inode *inode3, struct inode *inode4)
1e51764a 1271{
82c1593c
AB
1272 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1273 if (inode2 != inode1)
1274 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1275 if (inode3)
1276 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
9e0a1fff
RW
1277 if (inode4)
1278 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
1e51764a
AB
1279}
1280
1281/**
9e0a1fff 1282 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1e51764a
AB
1283 * @inode1: first inode
1284 * @inode2: second inode
1285 * @inode3: third inode
9e0a1fff 1286 * @inode4: fouth inode
1e51764a 1287 */
9e0a1fff
RW
1288static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1289 struct inode *inode3, struct inode *inode4)
1e51764a 1290{
9e0a1fff
RW
1291 if (inode4)
1292 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
1e51764a
AB
1293 if (inode3)
1294 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
82c1593c
AB
1295 if (inode1 != inode2)
1296 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1297 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1e51764a
AB
1298}
1299
390975ac
RW
1300static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1301 struct inode *new_dir, struct dentry *new_dentry,
1302 unsigned int flags)
1e51764a
AB
1303{
1304 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
2b0143b5
DH
1305 struct inode *old_inode = d_inode(old_dentry);
1306 struct inode *new_inode = d_inode(new_dentry);
9e0a1fff 1307 struct inode *whiteout = NULL;
1e51764a 1308 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
9e0a1fff 1309 struct ubifs_inode *whiteout_ui = NULL;
1e51764a
AB
1310 int err, release, sync = 0, move = (new_dir != old_dir);
1311 int is_dir = S_ISDIR(old_inode->i_mode);
f4f61d2c 1312 int unlink = !!new_inode, new_sz, old_sz;
1e51764a
AB
1313 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1314 .dirtied_ino = 3 };
1315 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
dab4b4d2 1316 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
1e51764a 1317 struct timespec time;
782759b9 1318 unsigned int uninitialized_var(saved_nlink);
f4f61d2c 1319 struct fscrypt_name old_nm, new_nm;
1e51764a
AB
1320
1321 /*
1322 * Budget request settings: deletion direntry, new direntry, removing
1323 * the old inode, and changing old and new parent directory inodes.
1324 *
1325 * However, this operation also marks the target inode as dirty and
1326 * does not write it, so we allocate budget for the target inode
1327 * separately.
1328 */
1329
9e0a1fff 1330 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
4cb2a01d 1331 old_dentry, old_inode->i_ino, old_dir->i_ino,
9e0a1fff
RW
1332 new_dentry, new_dir->i_ino, flags);
1333
82c1593c 1334 if (unlink)
5955102c 1335 ubifs_assert(inode_is_locked(new_inode));
82c1593c 1336
ac7e47a9
RW
1337 if (old_dir != new_dir) {
1338 if (ubifs_crypt_is_encrypted(new_dir) &&
1339 !fscrypt_has_permitted_context(new_dir, old_inode))
1340 return -EPERM;
1341 }
1342
1e51764a 1343 if (unlink && is_dir) {
f6337d84 1344 err = ubifs_check_dir_empty(new_inode);
1e51764a
AB
1345 if (err)
1346 return err;
1347 }
1348
f4f61d2c 1349 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
1e51764a
AB
1350 if (err)
1351 return err;
f4f61d2c
RW
1352
1353 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
1354 if (err) {
1355 fscrypt_free_filename(&old_nm);
1356 return err;
1357 }
1358
1359 new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
1360 old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
1361
1362 err = ubifs_budget_space(c, &req);
1363 if (err) {
1364 fscrypt_free_filename(&old_nm);
1365 fscrypt_free_filename(&new_nm);
1366 return err;
1367 }
1e51764a
AB
1368 err = ubifs_budget_space(c, &ino_req);
1369 if (err) {
f4f61d2c
RW
1370 fscrypt_free_filename(&old_nm);
1371 fscrypt_free_filename(&new_nm);
1e51764a
AB
1372 ubifs_release_budget(c, &req);
1373 return err;
1374 }
1375
9e0a1fff
RW
1376 if (flags & RENAME_WHITEOUT) {
1377 union ubifs_dev_desc *dev = NULL;
1378
1379 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1380 if (!dev) {
1381 ubifs_release_budget(c, &req);
1382 ubifs_release_budget(c, &ino_req);
1383 return -ENOMEM;
1384 }
1385
1386 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1387 if (err) {
1388 ubifs_release_budget(c, &req);
1389 ubifs_release_budget(c, &ino_req);
1390 kfree(dev);
1391 return err;
1392 }
1393
1394 whiteout->i_state |= I_LINKABLE;
1395 whiteout_ui = ubifs_inode(whiteout);
1396 whiteout_ui->data = dev;
1397 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
1398 ubifs_assert(!whiteout_ui->dirty);
1399 }
1400
1401 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1e51764a
AB
1402
1403 /*
1404 * Like most other Unix systems, set the @i_ctime for inodes on a
1405 * rename.
1406 */
1407 time = ubifs_current_time(old_dir);
1408 old_inode->i_ctime = time;
1409
1410 /* We must adjust parent link count when renaming directories */
1411 if (is_dir) {
1412 if (move) {
1413 /*
1414 * @old_dir loses a link because we are moving
1415 * @old_inode to a different directory.
1416 */
1417 drop_nlink(old_dir);
1418 /*
1419 * @new_dir only gains a link if we are not also
1420 * overwriting an existing directory.
1421 */
1422 if (!unlink)
1423 inc_nlink(new_dir);
1424 } else {
1425 /*
1426 * @old_inode is not moving to a different directory,
1427 * but @old_dir still loses a link if we are
1428 * overwriting an existing directory.
1429 */
1430 if (unlink)
1431 drop_nlink(old_dir);
1432 }
1433 }
1434
1435 old_dir->i_size -= old_sz;
1436 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1437 old_dir->i_mtime = old_dir->i_ctime = time;
1438 new_dir->i_mtime = new_dir->i_ctime = time;
1439
1440 /*
1441 * And finally, if we unlinked a direntry which happened to have the
1442 * same name as the moved direntry, we have to decrement @i_nlink of
1443 * the unlinked inode and change its ctime.
1444 */
1445 if (unlink) {
1446 /*
1447 * Directories cannot have hard-links, so if this is a
c43be108 1448 * directory, just clear @i_nlink.
1e51764a 1449 */
c43be108 1450 saved_nlink = new_inode->i_nlink;
1e51764a 1451 if (is_dir)
c43be108
AB
1452 clear_nlink(new_inode);
1453 else
1e51764a
AB
1454 drop_nlink(new_inode);
1455 new_inode->i_ctime = time;
1e51764a
AB
1456 } else {
1457 new_dir->i_size += new_sz;
1458 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1459 }
1460
1461 /*
1462 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1463 * is dirty, because this will be done later on at the end of
1464 * 'ubifs_rename()'.
1465 */
1466 if (IS_SYNC(old_inode)) {
1467 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1468 if (unlink && IS_SYNC(new_inode))
1469 sync = 1;
1470 }
9e0a1fff
RW
1471
1472 if (whiteout) {
1473 struct ubifs_budget_req wht_req = { .dirtied_ino = 1,
1474 .dirtied_ino_d = \
1475 ALIGN(ubifs_inode(whiteout)->data_len, 8) };
1476
1477 err = ubifs_budget_space(c, &wht_req);
1478 if (err) {
1479 ubifs_release_budget(c, &req);
1480 ubifs_release_budget(c, &ino_req);
1481 kfree(whiteout_ui->data);
1482 whiteout_ui->data_len = 0;
1483 iput(whiteout);
1484 return err;
1485 }
1486
1487 inc_nlink(whiteout);
1488 mark_inode_dirty(whiteout);
1489 whiteout->i_state &= ~I_LINKABLE;
1490 iput(whiteout);
1491 }
1492
f4f61d2c
RW
1493 err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
1494 new_inode, &new_nm, whiteout, sync);
1e51764a
AB
1495 if (err)
1496 goto out_cancel;
1497
9e0a1fff 1498 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1e51764a
AB
1499 ubifs_release_budget(c, &req);
1500
1501 mutex_lock(&old_inode_ui->ui_mutex);
1502 release = old_inode_ui->dirty;
1503 mark_inode_dirty_sync(old_inode);
1504 mutex_unlock(&old_inode_ui->ui_mutex);
1505
1506 if (release)
1507 ubifs_release_budget(c, &ino_req);
1508 if (IS_SYNC(old_inode))
a9185b41 1509 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
f4f61d2c
RW
1510
1511 fscrypt_free_filename(&old_nm);
1512 fscrypt_free_filename(&new_nm);
1e51764a
AB
1513 return err;
1514
1515out_cancel:
1516 if (unlink) {
c43be108 1517 set_nlink(new_inode, saved_nlink);
1e51764a
AB
1518 } else {
1519 new_dir->i_size -= new_sz;
1520 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1521 }
1522 old_dir->i_size += old_sz;
1523 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1524 if (is_dir) {
1525 if (move) {
1526 inc_nlink(old_dir);
1527 if (!unlink)
1528 drop_nlink(new_dir);
1529 } else {
1530 if (unlink)
1531 inc_nlink(old_dir);
1532 }
1533 }
9e0a1fff
RW
1534 if (whiteout) {
1535 drop_nlink(whiteout);
1536 iput(whiteout);
1537 }
1538 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1e51764a
AB
1539 ubifs_release_budget(c, &ino_req);
1540 ubifs_release_budget(c, &req);
f4f61d2c
RW
1541 fscrypt_free_filename(&old_nm);
1542 fscrypt_free_filename(&new_nm);
1e51764a
AB
1543 return err;
1544}
1545
9ec64962
RW
1546static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1547 struct inode *new_dir, struct dentry *new_dentry)
1548{
1549 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1550 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1551 .dirtied_ino = 2 };
1552 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1553 struct inode *fst_inode = d_inode(old_dentry);
1554 struct inode *snd_inode = d_inode(new_dentry);
1555 struct timespec time;
1556 int err;
f4f61d2c 1557 struct fscrypt_name fst_nm, snd_nm;
9ec64962
RW
1558
1559 ubifs_assert(fst_inode && snd_inode);
1560
ac7e47a9
RW
1561 if ((ubifs_crypt_is_encrypted(old_dir) ||
1562 ubifs_crypt_is_encrypted(new_dir)) &&
1563 (old_dir != new_dir) &&
1564 (!fscrypt_has_permitted_context(new_dir, fst_inode) ||
1565 !fscrypt_has_permitted_context(old_dir, snd_inode)))
1566 return -EPERM;
1567
f4f61d2c
RW
1568 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
1569 if (err)
1570 return err;
1571
1572 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
1573 if (err) {
1574 fscrypt_free_filename(&fst_nm);
1575 return err;
1576 }
1577
9ec64962
RW
1578 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1579
1580 time = ubifs_current_time(old_dir);
1581 fst_inode->i_ctime = time;
1582 snd_inode->i_ctime = time;
1583 old_dir->i_mtime = old_dir->i_ctime = time;
1584 new_dir->i_mtime = new_dir->i_ctime = time;
1585
1586 if (old_dir != new_dir) {
1587 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1588 inc_nlink(new_dir);
1589 drop_nlink(old_dir);
1590 }
1591 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1592 drop_nlink(new_dir);
1593 inc_nlink(old_dir);
1594 }
1595 }
1596
f4f61d2c
RW
1597 err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
1598 snd_inode, &snd_nm, sync);
9ec64962
RW
1599
1600 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1601 ubifs_release_budget(c, &req);
1602
f4f61d2c
RW
1603 fscrypt_free_filename(&fst_nm);
1604 fscrypt_free_filename(&snd_nm);
9ec64962
RW
1605 return err;
1606}
1607
390975ac 1608static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
9ec64962
RW
1609 struct inode *new_dir, struct dentry *new_dentry,
1610 unsigned int flags)
1611{
1612 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1613 return -EINVAL;
1614
1615 ubifs_assert(inode_is_locked(old_dir));
1616 ubifs_assert(inode_is_locked(new_dir));
1617
1618 if (flags & RENAME_EXCHANGE)
1619 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1620
390975ac 1621 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
9ec64962
RW
1622}
1623
a528d35e
DH
1624int ubifs_getattr(const struct path *path, struct kstat *stat,
1625 u32 request_mask, unsigned int flags)
1e51764a
AB
1626{
1627 loff_t size;
a528d35e 1628 struct inode *inode = d_inode(path->dentry);
1e51764a
AB
1629 struct ubifs_inode *ui = ubifs_inode(inode);
1630
1631 mutex_lock(&ui->ui_mutex);
6d42e7e9 1632 generic_fillattr(inode, stat);
1e51764a
AB
1633 stat->blksize = UBIFS_BLOCK_SIZE;
1634 stat->size = ui->ui_size;
1635
1636 /*
1637 * Unfortunately, the 'stat()' system call was designed for block
1638 * device based file systems, and it is not appropriate for UBIFS,
1639 * because UBIFS does not have notion of "block". For example, it is
1640 * difficult to tell how many block a directory takes - it actually
1641 * takes less than 300 bytes, but we have to round it to block size,
1642 * which introduces large mistake. This makes utilities like 'du' to
1643 * report completely senseless numbers. This is the reason why UBIFS
1644 * goes the same way as JFFS2 - it reports zero blocks for everything
1645 * but regular files, which makes more sense than reporting completely
1646 * wrong sizes.
1647 */
1648 if (S_ISREG(inode->i_mode)) {
1649 size = ui->xattr_size;
1650 size += stat->size;
1651 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1652 /*
1653 * Note, user-space expects 512-byte blocks count irrespectively
1654 * of what was reported in @stat->size.
1655 */
1656 stat->blocks = size >> 9;
1657 } else
1658 stat->blocks = 0;
1659 mutex_unlock(&ui->ui_mutex);
1660 return 0;
1661}
1662
ba40e6a3
RW
1663static int ubifs_dir_open(struct inode *dir, struct file *file)
1664{
1665 if (ubifs_crypt_is_encrypted(dir))
1666 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1667
1668 return 0;
1669}
1670
e8b81566 1671const struct inode_operations ubifs_dir_inode_operations = {
1e51764a
AB
1672 .lookup = ubifs_lookup,
1673 .create = ubifs_create,
1674 .link = ubifs_link,
1675 .symlink = ubifs_symlink,
1676 .unlink = ubifs_unlink,
1677 .mkdir = ubifs_mkdir,
1678 .rmdir = ubifs_rmdir,
1679 .mknod = ubifs_mknod,
390975ac 1680 .rename = ubifs_rename,
1e51764a
AB
1681 .setattr = ubifs_setattr,
1682 .getattr = ubifs_getattr,
1e51764a 1683 .listxattr = ubifs_listxattr,
8c1c5f26
DY
1684#ifdef CONFIG_UBIFS_ATIME_SUPPORT
1685 .update_time = ubifs_update_time,
1686#endif
474b9370 1687 .tmpfile = ubifs_tmpfile,
1e51764a
AB
1688};
1689
e8b81566 1690const struct file_operations ubifs_dir_operations = {
01122e06 1691 .llseek = generic_file_llseek,
1e51764a
AB
1692 .release = ubifs_dir_release,
1693 .read = generic_read_dir,
c51da20c 1694 .iterate_shared = ubifs_readdir,
1e51764a
AB
1695 .fsync = ubifs_fsync,
1696 .unlocked_ioctl = ubifs_ioctl,
ba40e6a3 1697 .open = ubifs_dir_open,
1e51764a
AB
1698#ifdef CONFIG_COMPAT
1699 .compat_ioctl = ubifs_compat_ioctl,
1700#endif
1701};