blob: 9257ee893bdb8cdafeb8fa312ec59526fbb209cf [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/* * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
6 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 * Authors: Artem Bityutskiy (Битюцкий Артём)
8 * Adrian Hunter
9 * Zoltan Sogor
10 */
11
12/*
13 * This file implements directory operations.
14 *
15 * All FS operations in this file allocate budget before writing anything to the
16 * media. If they fail to allocate it, the error is returned. The only
17 * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
18 * if they unable to allocate the budget, because deletion %-ENOSPC failure is
19 * not what users are usually ready to get. UBIFS budgeting subsystem has some
20 * space reserved for these purposes.
21 *
22 * All operations in this file write all inodes which they change straight
23 * away, instead of marking them dirty. For example, 'ubifs_link()' changes
24 * @i_size of the parent inode and writes the parent inode together with the
25 * target inode. This was done to simplify file-system recovery which would
26 * otherwise be very difficult to do. The only exception is rename which marks
27 * the re-named inode dirty (because its @i_ctime is updated) but does not
28 * write it, but just marks it as dirty.
29 */
30
31#include "ubifs.h"
32
33/**
34 * inherit_flags - inherit flags of the parent inode.
35 * @dir: parent inode
36 * @mode: new inode mode flags
37 *
38 * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
39 * parent directory inode @dir. UBIFS inodes inherit the following flags:
40 * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
41 * sub-directory basis;
42 * o %UBIFS_SYNC_FL - useful for the same reasons;
43 * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
44 *
45 * This function returns the inherited flags.
46 */
47static int inherit_flags(const struct inode *dir, umode_t mode)
48{
49 int flags;
50 const struct ubifs_inode *ui = ubifs_inode(dir);
51
52 if (!S_ISDIR(dir->i_mode))
53 /*
54 * The parent is not a directory, which means that an extended
55 * attribute inode is being created. No flags.
56 */
57 return 0;
58
59 flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
60 if (!S_ISDIR(mode))
61 /* The "DIRSYNC" flag only applies to directories */
62 flags &= ~UBIFS_DIRSYNC_FL;
63 return flags;
64}
65
66/**
67 * ubifs_new_inode - allocate new UBIFS inode object.
68 * @c: UBIFS file-system description object
69 * @dir: parent directory inode
70 * @mode: inode mode flags
71 *
72 * This function finds an unused inode number, allocates new inode and
73 * initializes it. Returns new inode in case of success and an error code in
74 * case of failure.
75 */
76struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
77 umode_t mode)
78{
79 int err;
80 struct inode *inode;
81 struct ubifs_inode *ui;
82 bool encrypted = false;
83
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 inode = new_inode(c->vfs_sb);
85 ui = ubifs_inode(inode);
86 if (!inode)
87 return ERR_PTR(-ENOMEM);
88
89 /*
90 * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
91 * marking them dirty in file write path (see 'file_update_time()').
92 * UBIFS has to fully control "clean <-> dirty" transitions of inodes
93 * to make budgeting work.
94 */
95 inode->i_flags |= S_NOCMTIME;
96
97 inode_init_owner(inode, dir, mode);
98 inode->i_mtime = inode->i_atime = inode->i_ctime =
99 current_time(inode);
100 inode->i_mapping->nrpages = 0;
101
Olivier Deprez157378f2022-04-04 15:47:50 +0200102 err = fscrypt_prepare_new_inode(dir, inode, &encrypted);
103 if (err) {
104 ubifs_err(c, "fscrypt_prepare_new_inode failed: %i", err);
105 goto out_iput;
106 }
107
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108 switch (mode & S_IFMT) {
109 case S_IFREG:
110 inode->i_mapping->a_ops = &ubifs_file_address_operations;
111 inode->i_op = &ubifs_file_inode_operations;
112 inode->i_fop = &ubifs_file_operations;
113 break;
114 case S_IFDIR:
115 inode->i_op = &ubifs_dir_inode_operations;
116 inode->i_fop = &ubifs_dir_operations;
117 inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
118 break;
119 case S_IFLNK:
120 inode->i_op = &ubifs_symlink_inode_operations;
121 break;
122 case S_IFSOCK:
123 case S_IFIFO:
124 case S_IFBLK:
125 case S_IFCHR:
126 inode->i_op = &ubifs_file_inode_operations;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000127 break;
128 default:
129 BUG();
130 }
131
132 ui->flags = inherit_flags(dir, mode);
133 ubifs_set_inode_flags(inode);
134 if (S_ISREG(mode))
135 ui->compr_type = c->default_compr;
136 else
137 ui->compr_type = UBIFS_COMPR_NONE;
138 ui->synced_i_size = 0;
139
140 spin_lock(&c->cnt_lock);
141 /* Inode number overflow is currently not supported */
142 if (c->highest_inum >= INUM_WARN_WATERMARK) {
143 if (c->highest_inum >= INUM_WATERMARK) {
144 spin_unlock(&c->cnt_lock);
145 ubifs_err(c, "out of inode numbers");
Olivier Deprez157378f2022-04-04 15:47:50 +0200146 err = -EINVAL;
147 goto out_iput;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 }
149 ubifs_warn(c, "running out of inode numbers (current %lu, max %u)",
150 (unsigned long)c->highest_inum, INUM_WATERMARK);
151 }
152
153 inode->i_ino = ++c->highest_inum;
154 /*
155 * The creation sequence number remains with this inode for its
156 * lifetime. All nodes for this inode have a greater sequence number,
157 * and so it is possible to distinguish obsolete nodes belonging to a
158 * previous incarnation of the same inode number - for example, for the
159 * purpose of rebuilding the index.
160 */
161 ui->creat_sqnum = ++c->max_sqnum;
162 spin_unlock(&c->cnt_lock);
163
164 if (encrypted) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200165 err = fscrypt_set_context(inode, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000166 if (err) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200167 ubifs_err(c, "fscrypt_set_context failed: %i", err);
168 goto out_iput;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169 }
170 }
171
172 return inode;
Olivier Deprez157378f2022-04-04 15:47:50 +0200173
174out_iput:
175 make_bad_inode(inode);
176 iput(inode);
177 return ERR_PTR(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178}
179
180static int dbg_check_name(const struct ubifs_info *c,
181 const struct ubifs_dent_node *dent,
182 const struct fscrypt_name *nm)
183{
184 if (!dbg_is_chk_gen(c))
185 return 0;
186 if (le16_to_cpu(dent->nlen) != fname_len(nm))
187 return -EINVAL;
188 if (memcmp(dent->name, fname_name(nm), fname_len(nm)))
189 return -EINVAL;
190 return 0;
191}
192
193static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
194 unsigned int flags)
195{
196 int err;
197 union ubifs_key key;
198 struct inode *inode = NULL;
199 struct ubifs_dent_node *dent = NULL;
200 struct ubifs_info *c = dir->i_sb->s_fs_info;
201 struct fscrypt_name nm;
202
203 dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino);
204
David Brazdil0f672f62019-12-10 10:32:29 +0000205 err = fscrypt_prepare_lookup(dir, dentry, &nm);
206 if (err == -ENOENT)
207 return d_splice_alias(NULL, dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000208 if (err)
209 return ERR_PTR(err);
210
211 if (fname_len(&nm) > UBIFS_MAX_NLEN) {
212 inode = ERR_PTR(-ENAMETOOLONG);
213 goto done;
214 }
215
216 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
217 if (!dent) {
218 inode = ERR_PTR(-ENOMEM);
219 goto done;
220 }
221
Olivier Deprez157378f2022-04-04 15:47:50 +0200222 if (fname_name(&nm) == NULL) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200223 if (nm.hash & ~UBIFS_S_KEY_HASH_MASK)
224 goto done; /* ENOENT */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000225 dent_key_init_hash(c, &key, dir->i_ino, nm.hash);
226 err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash);
227 } else {
228 dent_key_init(c, &key, dir->i_ino, &nm);
229 err = ubifs_tnc_lookup_nm(c, &key, dent, &nm);
230 }
231
232 if (err) {
233 if (err == -ENOENT)
234 dbg_gen("not found");
235 else
236 inode = ERR_PTR(err);
237 goto done;
238 }
239
240 if (dbg_check_name(c, dent, &nm)) {
241 inode = ERR_PTR(-EINVAL);
242 goto done;
243 }
244
245 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
246 if (IS_ERR(inode)) {
247 /*
248 * This should not happen. Probably the file-system needs
249 * checking.
250 */
251 err = PTR_ERR(inode);
252 ubifs_err(c, "dead directory entry '%pd', error %d",
253 dentry, err);
254 ubifs_ro_mode(c, err);
255 goto done;
256 }
257
Olivier Deprez157378f2022-04-04 15:47:50 +0200258 if (IS_ENCRYPTED(dir) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
260 !fscrypt_has_permitted_context(dir, inode)) {
261 ubifs_warn(c, "Inconsistent encryption contexts: %lu/%lu",
262 dir->i_ino, inode->i_ino);
263 iput(inode);
264 inode = ERR_PTR(-EPERM);
265 }
266
267done:
268 kfree(dent);
269 fscrypt_free_filename(&nm);
270 return d_splice_alias(inode, dentry);
271}
272
Olivier Deprez0e641232021-09-23 10:07:05 +0200273static int ubifs_prepare_create(struct inode *dir, struct dentry *dentry,
274 struct fscrypt_name *nm)
275{
276 if (fscrypt_is_nokey_name(dentry))
277 return -ENOKEY;
278
279 return fscrypt_setup_filename(dir, &dentry->d_name, 0, nm);
280}
281
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000282static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
283 bool excl)
284{
285 struct inode *inode;
286 struct ubifs_info *c = dir->i_sb->s_fs_info;
287 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
288 .dirtied_ino = 1 };
289 struct ubifs_inode *dir_ui = ubifs_inode(dir);
290 struct fscrypt_name nm;
291 int err, sz_change;
292
293 /*
294 * Budget request settings: new inode, new direntry, changing the
295 * parent directory inode.
296 */
297
298 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
299 dentry, mode, dir->i_ino);
300
301 err = ubifs_budget_space(c, &req);
302 if (err)
303 return err;
304
Olivier Deprez0e641232021-09-23 10:07:05 +0200305 err = ubifs_prepare_create(dir, dentry, &nm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306 if (err)
307 goto out_budg;
308
309 sz_change = CALC_DENT_SIZE(fname_len(&nm));
310
311 inode = ubifs_new_inode(c, dir, mode);
312 if (IS_ERR(inode)) {
313 err = PTR_ERR(inode);
314 goto out_fname;
315 }
316
317 err = ubifs_init_security(dir, inode, &dentry->d_name);
318 if (err)
319 goto out_inode;
320
321 mutex_lock(&dir_ui->ui_mutex);
322 dir->i_size += sz_change;
323 dir_ui->ui_size = dir->i_size;
324 dir->i_mtime = dir->i_ctime = inode->i_ctime;
325 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
326 if (err)
327 goto out_cancel;
328 mutex_unlock(&dir_ui->ui_mutex);
329
330 ubifs_release_budget(c, &req);
331 fscrypt_free_filename(&nm);
332 insert_inode_hash(inode);
333 d_instantiate(dentry, inode);
334 return 0;
335
336out_cancel:
337 dir->i_size -= sz_change;
338 dir_ui->ui_size = dir->i_size;
339 mutex_unlock(&dir_ui->ui_mutex);
340out_inode:
341 make_bad_inode(inode);
342 iput(inode);
343out_fname:
344 fscrypt_free_filename(&nm);
345out_budg:
346 ubifs_release_budget(c, &req);
347 ubifs_err(c, "cannot create regular file, error %d", err);
348 return err;
349}
350
351static int do_tmpfile(struct inode *dir, struct dentry *dentry,
352 umode_t mode, struct inode **whiteout)
353{
354 struct inode *inode;
355 struct ubifs_info *c = dir->i_sb->s_fs_info;
Olivier Deprez92d4c212022-12-06 15:05:30 +0100356 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
357 .dirtied_ino = 1};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358 struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
359 struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
360 int err, instantiated = 0;
361 struct fscrypt_name nm;
362
363 /*
Olivier Deprez92d4c212022-12-06 15:05:30 +0100364 * Budget request settings: new inode, new direntry, changing the
365 * parent directory inode.
366 * Allocate budget separately for new dirtied inode, the budget will
367 * be released via writeback.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000368 */
369
370 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
371 dentry, mode, dir->i_ino);
372
373 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
374 if (err)
375 return err;
376
377 err = ubifs_budget_space(c, &req);
378 if (err) {
379 fscrypt_free_filename(&nm);
380 return err;
381 }
382
383 err = ubifs_budget_space(c, &ino_req);
384 if (err) {
385 ubifs_release_budget(c, &req);
386 fscrypt_free_filename(&nm);
387 return err;
388 }
389
390 inode = ubifs_new_inode(c, dir, mode);
391 if (IS_ERR(inode)) {
392 err = PTR_ERR(inode);
393 goto out_budg;
394 }
395 ui = ubifs_inode(inode);
396
397 if (whiteout) {
398 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
399 ubifs_assert(c, inode->i_op == &ubifs_file_inode_operations);
400 }
401
402 err = ubifs_init_security(dir, inode, &dentry->d_name);
403 if (err)
404 goto out_inode;
405
406 mutex_lock(&ui->ui_mutex);
407 insert_inode_hash(inode);
408
409 if (whiteout) {
410 mark_inode_dirty(inode);
411 drop_nlink(inode);
412 *whiteout = inode;
413 } else {
414 d_tmpfile(dentry, inode);
415 }
416 ubifs_assert(c, ui->dirty);
417
418 instantiated = 1;
419 mutex_unlock(&ui->ui_mutex);
420
421 mutex_lock(&dir_ui->ui_mutex);
422 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
423 if (err)
424 goto out_cancel;
425 mutex_unlock(&dir_ui->ui_mutex);
426
427 ubifs_release_budget(c, &req);
428
429 return 0;
430
431out_cancel:
432 mutex_unlock(&dir_ui->ui_mutex);
433out_inode:
434 make_bad_inode(inode);
435 if (!instantiated)
436 iput(inode);
Olivier Deprez92d4c212022-12-06 15:05:30 +0100437 else if (whiteout)
438 iput(*whiteout);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439out_budg:
440 ubifs_release_budget(c, &req);
441 if (!instantiated)
442 ubifs_release_budget(c, &ino_req);
443 fscrypt_free_filename(&nm);
444 ubifs_err(c, "cannot create temporary file, error %d", err);
445 return err;
446}
447
448static int ubifs_tmpfile(struct inode *dir, struct dentry *dentry,
449 umode_t mode)
450{
451 return do_tmpfile(dir, dentry, mode, NULL);
452}
453
454/**
455 * vfs_dent_type - get VFS directory entry type.
456 * @type: UBIFS directory entry type
457 *
458 * This function converts UBIFS directory entry type into VFS directory entry
459 * type.
460 */
461static unsigned int vfs_dent_type(uint8_t type)
462{
463 switch (type) {
464 case UBIFS_ITYPE_REG:
465 return DT_REG;
466 case UBIFS_ITYPE_DIR:
467 return DT_DIR;
468 case UBIFS_ITYPE_LNK:
469 return DT_LNK;
470 case UBIFS_ITYPE_BLK:
471 return DT_BLK;
472 case UBIFS_ITYPE_CHR:
473 return DT_CHR;
474 case UBIFS_ITYPE_FIFO:
475 return DT_FIFO;
476 case UBIFS_ITYPE_SOCK:
477 return DT_SOCK;
478 default:
479 BUG();
480 }
481 return 0;
482}
483
484/*
485 * The classical Unix view for directory is that it is a linear array of
486 * (name, inode number) entries. Linux/VFS assumes this model as well.
487 * Particularly, 'readdir()' call wants us to return a directory entry offset
488 * which later may be used to continue 'readdir()'ing the directory or to
489 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
490 * model because directory entries are identified by keys, which may collide.
491 *
492 * UBIFS uses directory entry hash value for directory offsets, so
493 * 'seekdir()'/'telldir()' may not always work because of possible key
494 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
495 * properly by means of saving full directory entry name in the private field
496 * of the file description object.
497 *
498 * This means that UBIFS cannot support NFS which requires full
499 * 'seekdir()'/'telldir()' support.
500 */
501static int ubifs_readdir(struct file *file, struct dir_context *ctx)
502{
503 int fstr_real_len = 0, err = 0;
504 struct fscrypt_name nm;
505 struct fscrypt_str fstr = {0};
506 union ubifs_key key;
507 struct ubifs_dent_node *dent;
508 struct inode *dir = file_inode(file);
509 struct ubifs_info *c = dir->i_sb->s_fs_info;
Olivier Deprez157378f2022-04-04 15:47:50 +0200510 bool encrypted = IS_ENCRYPTED(dir);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000511
512 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos);
513
514 if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2)
515 /*
516 * The directory was seek'ed to a senseless position or there
517 * are no more entries.
518 */
519 return 0;
520
521 if (encrypted) {
522 err = fscrypt_get_encryption_info(dir);
Olivier Deprez157378f2022-04-04 15:47:50 +0200523 if (err)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000524 return err;
525
Olivier Deprez157378f2022-04-04 15:47:50 +0200526 err = fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN, &fstr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000527 if (err)
528 return err;
529
530 fstr_real_len = fstr.len;
531 }
532
533 if (file->f_version == 0) {
534 /*
535 * The file was seek'ed, which means that @file->private_data
536 * is now invalid. This may also be just the first
537 * 'ubifs_readdir()' invocation, in which case
538 * @file->private_data is NULL, and the below code is
539 * basically a no-op.
540 */
541 kfree(file->private_data);
542 file->private_data = NULL;
543 }
544
545 /*
546 * 'generic_file_llseek()' unconditionally sets @file->f_version to
547 * zero, and we use this for detecting whether the file was seek'ed.
548 */
549 file->f_version = 1;
550
551 /* File positions 0 and 1 correspond to "." and ".." */
552 if (ctx->pos < 2) {
553 ubifs_assert(c, !file->private_data);
554 if (!dir_emit_dots(file, ctx)) {
555 if (encrypted)
556 fscrypt_fname_free_buffer(&fstr);
557 return 0;
558 }
559
560 /* Find the first entry in TNC and save it */
561 lowest_dent_key(c, &key, dir->i_ino);
562 fname_len(&nm) = 0;
563 dent = ubifs_tnc_next_ent(c, &key, &nm);
564 if (IS_ERR(dent)) {
565 err = PTR_ERR(dent);
566 goto out;
567 }
568
569 ctx->pos = key_hash_flash(c, &dent->key);
570 file->private_data = dent;
571 }
572
573 dent = file->private_data;
574 if (!dent) {
575 /*
576 * The directory was seek'ed to and is now readdir'ed.
577 * Find the entry corresponding to @ctx->pos or the closest one.
578 */
579 dent_key_init_hash(c, &key, dir->i_ino, ctx->pos);
580 fname_len(&nm) = 0;
581 dent = ubifs_tnc_next_ent(c, &key, &nm);
582 if (IS_ERR(dent)) {
583 err = PTR_ERR(dent);
584 goto out;
585 }
586 ctx->pos = key_hash_flash(c, &dent->key);
587 file->private_data = dent;
588 }
589
590 while (1) {
591 dbg_gen("ino %llu, new f_pos %#x",
592 (unsigned long long)le64_to_cpu(dent->inum),
593 key_hash_flash(c, &dent->key));
594 ubifs_assert(c, le64_to_cpu(dent->ch.sqnum) >
595 ubifs_inode(dir)->creat_sqnum);
596
597 fname_len(&nm) = le16_to_cpu(dent->nlen);
598 fname_name(&nm) = dent->name;
599
600 if (encrypted) {
601 fstr.len = fstr_real_len;
602
603 err = fscrypt_fname_disk_to_usr(dir, key_hash_flash(c,
604 &dent->key),
605 le32_to_cpu(dent->cookie),
606 &nm.disk_name, &fstr);
607 if (err)
608 goto out;
609 } else {
610 fstr.len = fname_len(&nm);
611 fstr.name = fname_name(&nm);
612 }
613
614 if (!dir_emit(ctx, fstr.name, fstr.len,
615 le64_to_cpu(dent->inum),
616 vfs_dent_type(dent->type))) {
617 if (encrypted)
618 fscrypt_fname_free_buffer(&fstr);
619 return 0;
620 }
621
622 /* Switch to the next entry */
623 key_read(c, &dent->key, &key);
624 dent = ubifs_tnc_next_ent(c, &key, &nm);
625 if (IS_ERR(dent)) {
626 err = PTR_ERR(dent);
627 goto out;
628 }
629
630 kfree(file->private_data);
631 ctx->pos = key_hash_flash(c, &dent->key);
632 file->private_data = dent;
633 cond_resched();
634 }
635
636out:
637 kfree(file->private_data);
638 file->private_data = NULL;
639
640 if (encrypted)
641 fscrypt_fname_free_buffer(&fstr);
642
643 if (err != -ENOENT)
644 ubifs_err(c, "cannot find next direntry, error %d", err);
645 else
646 /*
647 * -ENOENT is a non-fatal error in this context, the TNC uses
648 * it to indicate that the cursor moved past the current directory
649 * and readdir() has to stop.
650 */
651 err = 0;
652
653
654 /* 2 is a special value indicating that there are no more direntries */
655 ctx->pos = 2;
656 return err;
657}
658
659/* Free saved readdir() state when the directory is closed */
660static int ubifs_dir_release(struct inode *dir, struct file *file)
661{
662 kfree(file->private_data);
663 file->private_data = NULL;
664 return 0;
665}
666
667/**
668 * lock_2_inodes - a wrapper for locking two UBIFS inodes.
669 * @inode1: first inode
670 * @inode2: second inode
671 *
672 * We do not implement any tricks to guarantee strict lock ordering, because
673 * VFS has already done it for us on the @i_mutex. So this is just a simple
674 * wrapper function.
675 */
676static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
677{
678 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
679 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
680}
681
682/**
683 * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
684 * @inode1: first inode
685 * @inode2: second inode
686 */
687static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
688{
689 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
690 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
691}
692
693static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
694 struct dentry *dentry)
695{
696 struct ubifs_info *c = dir->i_sb->s_fs_info;
697 struct inode *inode = d_inode(old_dentry);
698 struct ubifs_inode *ui = ubifs_inode(inode);
699 struct ubifs_inode *dir_ui = ubifs_inode(dir);
700 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
701 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
702 .dirtied_ino_d = ALIGN(ui->data_len, 8) };
703 struct fscrypt_name nm;
704
705 /*
706 * Budget request settings: new direntry, changing the target inode,
707 * changing the parent inode.
708 */
709
710 dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu",
711 dentry, inode->i_ino,
712 inode->i_nlink, dir->i_ino);
713 ubifs_assert(c, inode_is_locked(dir));
714 ubifs_assert(c, inode_is_locked(inode));
715
716 err = fscrypt_prepare_link(old_dentry, dir, dentry);
717 if (err)
718 return err;
719
720 err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm);
721 if (err)
722 return err;
723
724 err = dbg_check_synced_i_size(c, inode);
725 if (err)
726 goto out_fname;
727
728 err = ubifs_budget_space(c, &req);
729 if (err)
730 goto out_fname;
731
732 lock_2_inodes(dir, inode);
733
734 /* Handle O_TMPFILE corner case, it is allowed to link a O_TMPFILE. */
735 if (inode->i_nlink == 0)
736 ubifs_delete_orphan(c, inode->i_ino);
737
738 inc_nlink(inode);
739 ihold(inode);
740 inode->i_ctime = current_time(inode);
741 dir->i_size += sz_change;
742 dir_ui->ui_size = dir->i_size;
743 dir->i_mtime = dir->i_ctime = inode->i_ctime;
744 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
745 if (err)
746 goto out_cancel;
747 unlock_2_inodes(dir, inode);
748
749 ubifs_release_budget(c, &req);
750 d_instantiate(dentry, inode);
751 fscrypt_free_filename(&nm);
752 return 0;
753
754out_cancel:
755 dir->i_size -= sz_change;
756 dir_ui->ui_size = dir->i_size;
757 drop_nlink(inode);
758 if (inode->i_nlink == 0)
759 ubifs_add_orphan(c, inode->i_ino);
760 unlock_2_inodes(dir, inode);
761 ubifs_release_budget(c, &req);
762 iput(inode);
763out_fname:
764 fscrypt_free_filename(&nm);
765 return err;
766}
767
768static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
769{
770 struct ubifs_info *c = dir->i_sb->s_fs_info;
771 struct inode *inode = d_inode(dentry);
772 struct ubifs_inode *dir_ui = ubifs_inode(dir);
773 int err, sz_change, budgeted = 1;
774 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
775 unsigned int saved_nlink = inode->i_nlink;
776 struct fscrypt_name nm;
777
778 /*
779 * Budget request settings: deletion direntry, deletion inode (+1 for
780 * @dirtied_ino), changing the parent directory inode. If budgeting
781 * fails, go ahead anyway because we have extra space reserved for
782 * deletions.
783 */
784
785 dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu",
786 dentry, inode->i_ino,
787 inode->i_nlink, dir->i_ino);
788
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
790 if (err)
791 return err;
792
David Brazdil0f672f62019-12-10 10:32:29 +0000793 err = ubifs_purge_xattrs(inode);
794 if (err)
795 return err;
796
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000797 sz_change = CALC_DENT_SIZE(fname_len(&nm));
798
799 ubifs_assert(c, inode_is_locked(dir));
800 ubifs_assert(c, inode_is_locked(inode));
801 err = dbg_check_synced_i_size(c, inode);
802 if (err)
803 goto out_fname;
804
805 err = ubifs_budget_space(c, &req);
806 if (err) {
807 if (err != -ENOSPC)
808 goto out_fname;
809 budgeted = 0;
810 }
811
812 lock_2_inodes(dir, inode);
813 inode->i_ctime = current_time(dir);
814 drop_nlink(inode);
815 dir->i_size -= sz_change;
816 dir_ui->ui_size = dir->i_size;
817 dir->i_mtime = dir->i_ctime = inode->i_ctime;
818 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
819 if (err)
820 goto out_cancel;
821 unlock_2_inodes(dir, inode);
822
823 if (budgeted)
824 ubifs_release_budget(c, &req);
825 else {
826 /* We've deleted something - clean the "no space" flags */
827 c->bi.nospace = c->bi.nospace_rp = 0;
828 smp_wmb();
829 }
830 fscrypt_free_filename(&nm);
831 return 0;
832
833out_cancel:
834 dir->i_size += sz_change;
835 dir_ui->ui_size = dir->i_size;
836 set_nlink(inode, saved_nlink);
837 unlock_2_inodes(dir, inode);
838 if (budgeted)
839 ubifs_release_budget(c, &req);
840out_fname:
841 fscrypt_free_filename(&nm);
842 return err;
843}
844
845/**
846 * check_dir_empty - check if a directory is empty or not.
847 * @dir: VFS inode object of the directory to check
848 *
849 * This function checks if directory @dir is empty. Returns zero if the
850 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
851 * in case of of errors.
852 */
853int ubifs_check_dir_empty(struct inode *dir)
854{
855 struct ubifs_info *c = dir->i_sb->s_fs_info;
856 struct fscrypt_name nm = { 0 };
857 struct ubifs_dent_node *dent;
858 union ubifs_key key;
859 int err;
860
861 lowest_dent_key(c, &key, dir->i_ino);
862 dent = ubifs_tnc_next_ent(c, &key, &nm);
863 if (IS_ERR(dent)) {
864 err = PTR_ERR(dent);
865 if (err == -ENOENT)
866 err = 0;
867 } else {
868 kfree(dent);
869 err = -ENOTEMPTY;
870 }
871 return err;
872}
873
874static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
875{
876 struct ubifs_info *c = dir->i_sb->s_fs_info;
877 struct inode *inode = d_inode(dentry);
878 int err, sz_change, budgeted = 1;
879 struct ubifs_inode *dir_ui = ubifs_inode(dir);
880 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
881 struct fscrypt_name nm;
882
883 /*
884 * Budget request settings: deletion direntry, deletion inode and
885 * changing the parent inode. If budgeting fails, go ahead anyway
886 * because we have extra space reserved for deletions.
887 */
888
889 dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry,
890 inode->i_ino, dir->i_ino);
891 ubifs_assert(c, inode_is_locked(dir));
892 ubifs_assert(c, inode_is_locked(inode));
893 err = ubifs_check_dir_empty(d_inode(dentry));
894 if (err)
895 return err;
896
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000897 err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm);
898 if (err)
899 return err;
900
David Brazdil0f672f62019-12-10 10:32:29 +0000901 err = ubifs_purge_xattrs(inode);
902 if (err)
903 return err;
904
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000905 sz_change = CALC_DENT_SIZE(fname_len(&nm));
906
907 err = ubifs_budget_space(c, &req);
908 if (err) {
909 if (err != -ENOSPC)
910 goto out_fname;
911 budgeted = 0;
912 }
913
914 lock_2_inodes(dir, inode);
915 inode->i_ctime = current_time(dir);
916 clear_nlink(inode);
917 drop_nlink(dir);
918 dir->i_size -= sz_change;
919 dir_ui->ui_size = dir->i_size;
920 dir->i_mtime = dir->i_ctime = inode->i_ctime;
921 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
922 if (err)
923 goto out_cancel;
924 unlock_2_inodes(dir, inode);
925
926 if (budgeted)
927 ubifs_release_budget(c, &req);
928 else {
929 /* We've deleted something - clean the "no space" flags */
930 c->bi.nospace = c->bi.nospace_rp = 0;
931 smp_wmb();
932 }
933 fscrypt_free_filename(&nm);
934 return 0;
935
936out_cancel:
937 dir->i_size += sz_change;
938 dir_ui->ui_size = dir->i_size;
939 inc_nlink(dir);
940 set_nlink(inode, 2);
941 unlock_2_inodes(dir, inode);
942 if (budgeted)
943 ubifs_release_budget(c, &req);
944out_fname:
945 fscrypt_free_filename(&nm);
946 return err;
947}
948
949static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
950{
951 struct inode *inode;
952 struct ubifs_inode *dir_ui = ubifs_inode(dir);
953 struct ubifs_info *c = dir->i_sb->s_fs_info;
954 int err, sz_change;
Olivier Deprez92d4c212022-12-06 15:05:30 +0100955 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
956 .dirtied_ino = 1};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000957 struct fscrypt_name nm;
958
959 /*
960 * Budget request settings: new inode, new direntry and changing parent
961 * directory inode.
962 */
963
964 dbg_gen("dent '%pd', mode %#hx in dir ino %lu",
965 dentry, mode, dir->i_ino);
966
967 err = ubifs_budget_space(c, &req);
968 if (err)
969 return err;
970
Olivier Deprez0e641232021-09-23 10:07:05 +0200971 err = ubifs_prepare_create(dir, dentry, &nm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000972 if (err)
973 goto out_budg;
974
975 sz_change = CALC_DENT_SIZE(fname_len(&nm));
976
977 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
978 if (IS_ERR(inode)) {
979 err = PTR_ERR(inode);
980 goto out_fname;
981 }
982
983 err = ubifs_init_security(dir, inode, &dentry->d_name);
984 if (err)
985 goto out_inode;
986
987 mutex_lock(&dir_ui->ui_mutex);
988 insert_inode_hash(inode);
989 inc_nlink(inode);
990 inc_nlink(dir);
991 dir->i_size += sz_change;
992 dir_ui->ui_size = dir->i_size;
993 dir->i_mtime = dir->i_ctime = inode->i_ctime;
994 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
995 if (err) {
996 ubifs_err(c, "cannot create directory, error %d", err);
997 goto out_cancel;
998 }
999 mutex_unlock(&dir_ui->ui_mutex);
1000
1001 ubifs_release_budget(c, &req);
1002 d_instantiate(dentry, inode);
1003 fscrypt_free_filename(&nm);
1004 return 0;
1005
1006out_cancel:
1007 dir->i_size -= sz_change;
1008 dir_ui->ui_size = dir->i_size;
1009 drop_nlink(dir);
1010 mutex_unlock(&dir_ui->ui_mutex);
1011out_inode:
1012 make_bad_inode(inode);
1013 iput(inode);
1014out_fname:
1015 fscrypt_free_filename(&nm);
1016out_budg:
1017 ubifs_release_budget(c, &req);
1018 return err;
1019}
1020
1021static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
1022 umode_t mode, dev_t rdev)
1023{
1024 struct inode *inode;
1025 struct ubifs_inode *ui;
1026 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1027 struct ubifs_info *c = dir->i_sb->s_fs_info;
1028 union ubifs_dev_desc *dev = NULL;
1029 int sz_change;
1030 int err, devlen = 0;
1031 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1032 .dirtied_ino = 1 };
1033 struct fscrypt_name nm;
1034
1035 /*
1036 * Budget request settings: new inode, new direntry and changing parent
1037 * directory inode.
1038 */
1039
1040 dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino);
1041
1042 if (S_ISBLK(mode) || S_ISCHR(mode)) {
1043 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1044 if (!dev)
1045 return -ENOMEM;
1046 devlen = ubifs_encode_dev(dev, rdev);
1047 }
1048
1049 req.new_ino_d = ALIGN(devlen, 8);
1050 err = ubifs_budget_space(c, &req);
1051 if (err) {
1052 kfree(dev);
1053 return err;
1054 }
1055
Olivier Deprez0e641232021-09-23 10:07:05 +02001056 err = ubifs_prepare_create(dir, dentry, &nm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001057 if (err) {
1058 kfree(dev);
1059 goto out_budg;
1060 }
1061
1062 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1063
1064 inode = ubifs_new_inode(c, dir, mode);
1065 if (IS_ERR(inode)) {
1066 kfree(dev);
1067 err = PTR_ERR(inode);
1068 goto out_fname;
1069 }
1070
1071 init_special_inode(inode, inode->i_mode, rdev);
1072 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
1073 ui = ubifs_inode(inode);
1074 ui->data = dev;
1075 ui->data_len = devlen;
1076
1077 err = ubifs_init_security(dir, inode, &dentry->d_name);
1078 if (err)
1079 goto out_inode;
1080
1081 mutex_lock(&dir_ui->ui_mutex);
1082 dir->i_size += sz_change;
1083 dir_ui->ui_size = dir->i_size;
1084 dir->i_mtime = dir->i_ctime = inode->i_ctime;
1085 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1086 if (err)
1087 goto out_cancel;
1088 mutex_unlock(&dir_ui->ui_mutex);
1089
1090 ubifs_release_budget(c, &req);
1091 insert_inode_hash(inode);
1092 d_instantiate(dentry, inode);
1093 fscrypt_free_filename(&nm);
1094 return 0;
1095
1096out_cancel:
1097 dir->i_size -= sz_change;
1098 dir_ui->ui_size = dir->i_size;
1099 mutex_unlock(&dir_ui->ui_mutex);
1100out_inode:
1101 make_bad_inode(inode);
1102 iput(inode);
1103out_fname:
1104 fscrypt_free_filename(&nm);
1105out_budg:
1106 ubifs_release_budget(c, &req);
1107 return err;
1108}
1109
1110static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
1111 const char *symname)
1112{
1113 struct inode *inode;
1114 struct ubifs_inode *ui;
1115 struct ubifs_inode *dir_ui = ubifs_inode(dir);
1116 struct ubifs_info *c = dir->i_sb->s_fs_info;
1117 int err, sz_change, len = strlen(symname);
1118 struct fscrypt_str disk_link;
1119 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
1120 .new_ino_d = ALIGN(len, 8),
1121 .dirtied_ino = 1 };
1122 struct fscrypt_name nm;
1123
1124 dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry,
1125 symname, dir->i_ino);
1126
1127 err = fscrypt_prepare_symlink(dir, symname, len, UBIFS_MAX_INO_DATA,
1128 &disk_link);
1129 if (err)
1130 return err;
1131
1132 /*
1133 * Budget request settings: new inode, new direntry and changing parent
1134 * directory inode.
1135 */
1136 err = ubifs_budget_space(c, &req);
1137 if (err)
1138 return err;
1139
Olivier Deprez0e641232021-09-23 10:07:05 +02001140 err = ubifs_prepare_create(dir, dentry, &nm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001141 if (err)
1142 goto out_budg;
1143
1144 sz_change = CALC_DENT_SIZE(fname_len(&nm));
1145
1146 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
1147 if (IS_ERR(inode)) {
1148 err = PTR_ERR(inode);
1149 goto out_fname;
1150 }
1151
1152 ui = ubifs_inode(inode);
1153 ui->data = kmalloc(disk_link.len, GFP_NOFS);
1154 if (!ui->data) {
1155 err = -ENOMEM;
1156 goto out_inode;
1157 }
1158
1159 if (IS_ENCRYPTED(inode)) {
1160 disk_link.name = ui->data; /* encrypt directly into ui->data */
1161 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
1162 if (err)
1163 goto out_inode;
1164 } else {
1165 memcpy(ui->data, disk_link.name, disk_link.len);
1166 inode->i_link = ui->data;
1167 }
1168
1169 /*
1170 * The terminating zero byte is not written to the flash media and it
1171 * is put just to make later in-memory string processing simpler. Thus,
1172 * data length is @disk_link.len - 1, not @disk_link.len.
1173 */
1174 ui->data_len = disk_link.len - 1;
1175 inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1;
1176
1177 err = ubifs_init_security(dir, inode, &dentry->d_name);
1178 if (err)
1179 goto out_inode;
1180
1181 mutex_lock(&dir_ui->ui_mutex);
1182 dir->i_size += sz_change;
1183 dir_ui->ui_size = dir->i_size;
1184 dir->i_mtime = dir->i_ctime = inode->i_ctime;
1185 err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0);
1186 if (err)
1187 goto out_cancel;
1188 mutex_unlock(&dir_ui->ui_mutex);
1189
1190 insert_inode_hash(inode);
1191 d_instantiate(dentry, inode);
1192 err = 0;
1193 goto out_fname;
1194
1195out_cancel:
1196 dir->i_size -= sz_change;
1197 dir_ui->ui_size = dir->i_size;
1198 mutex_unlock(&dir_ui->ui_mutex);
1199out_inode:
1200 make_bad_inode(inode);
1201 iput(inode);
1202out_fname:
1203 fscrypt_free_filename(&nm);
1204out_budg:
1205 ubifs_release_budget(c, &req);
1206 return err;
1207}
1208
1209/**
1210 * lock_4_inodes - a wrapper for locking three UBIFS inodes.
1211 * @inode1: first inode
1212 * @inode2: second inode
1213 * @inode3: third inode
1214 * @inode4: fouth inode
1215 *
1216 * This function is used for 'ubifs_rename()' and @inode1 may be the same as
1217 * @inode2 whereas @inode3 and @inode4 may be %NULL.
1218 *
1219 * We do not implement any tricks to guarantee strict lock ordering, because
1220 * VFS has already done it for us on the @i_mutex. So this is just a simple
1221 * wrapper function.
1222 */
1223static void lock_4_inodes(struct inode *inode1, struct inode *inode2,
1224 struct inode *inode3, struct inode *inode4)
1225{
1226 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
1227 if (inode2 != inode1)
1228 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
1229 if (inode3)
1230 mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3);
1231 if (inode4)
1232 mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4);
1233}
1234
1235/**
1236 * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename.
1237 * @inode1: first inode
1238 * @inode2: second inode
1239 * @inode3: third inode
1240 * @inode4: fouth inode
1241 */
1242static void unlock_4_inodes(struct inode *inode1, struct inode *inode2,
1243 struct inode *inode3, struct inode *inode4)
1244{
1245 if (inode4)
1246 mutex_unlock(&ubifs_inode(inode4)->ui_mutex);
1247 if (inode3)
1248 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
1249 if (inode1 != inode2)
1250 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
1251 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
1252}
1253
1254static int do_rename(struct inode *old_dir, struct dentry *old_dentry,
1255 struct inode *new_dir, struct dentry *new_dentry,
1256 unsigned int flags)
1257{
1258 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1259 struct inode *old_inode = d_inode(old_dentry);
1260 struct inode *new_inode = d_inode(new_dentry);
1261 struct inode *whiteout = NULL;
1262 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
1263 struct ubifs_inode *whiteout_ui = NULL;
1264 int err, release, sync = 0, move = (new_dir != old_dir);
1265 int is_dir = S_ISDIR(old_inode->i_mode);
1266 int unlink = !!new_inode, new_sz, old_sz;
1267 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1268 .dirtied_ino = 3 };
1269 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
1270 .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) };
1271 struct timespec64 time;
Olivier Deprez157378f2022-04-04 15:47:50 +02001272 unsigned int saved_nlink;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001273 struct fscrypt_name old_nm, new_nm;
1274
1275 /*
1276 * Budget request settings: deletion direntry, new direntry, removing
1277 * the old inode, and changing old and new parent directory inodes.
1278 *
1279 * However, this operation also marks the target inode as dirty and
1280 * does not write it, so we allocate budget for the target inode
1281 * separately.
1282 */
1283
1284 dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x",
1285 old_dentry, old_inode->i_ino, old_dir->i_ino,
1286 new_dentry, new_dir->i_ino, flags);
1287
David Brazdil0f672f62019-12-10 10:32:29 +00001288 if (unlink) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001289 ubifs_assert(c, inode_is_locked(new_inode));
1290
David Brazdil0f672f62019-12-10 10:32:29 +00001291 err = ubifs_purge_xattrs(new_inode);
1292 if (err)
1293 return err;
1294 }
1295
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001296 if (unlink && is_dir) {
1297 err = ubifs_check_dir_empty(new_inode);
1298 if (err)
1299 return err;
1300 }
1301
1302 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm);
1303 if (err)
1304 return err;
1305
1306 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_nm);
1307 if (err) {
1308 fscrypt_free_filename(&old_nm);
1309 return err;
1310 }
1311
1312 new_sz = CALC_DENT_SIZE(fname_len(&new_nm));
1313 old_sz = CALC_DENT_SIZE(fname_len(&old_nm));
1314
1315 err = ubifs_budget_space(c, &req);
1316 if (err) {
1317 fscrypt_free_filename(&old_nm);
1318 fscrypt_free_filename(&new_nm);
1319 return err;
1320 }
1321 err = ubifs_budget_space(c, &ino_req);
1322 if (err) {
1323 fscrypt_free_filename(&old_nm);
1324 fscrypt_free_filename(&new_nm);
1325 ubifs_release_budget(c, &req);
1326 return err;
1327 }
1328
1329 if (flags & RENAME_WHITEOUT) {
1330 union ubifs_dev_desc *dev = NULL;
Olivier Deprez92d4c212022-12-06 15:05:30 +01001331 struct ubifs_budget_req wht_req;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001332
1333 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
1334 if (!dev) {
1335 err = -ENOMEM;
1336 goto out_release;
1337 }
1338
1339 err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout);
1340 if (err) {
1341 kfree(dev);
1342 goto out_release;
1343 }
1344
Olivier Deprez0e641232021-09-23 10:07:05 +02001345 spin_lock(&whiteout->i_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001346 whiteout->i_state |= I_LINKABLE;
Olivier Deprez0e641232021-09-23 10:07:05 +02001347 spin_unlock(&whiteout->i_lock);
1348
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001349 whiteout_ui = ubifs_inode(whiteout);
1350 whiteout_ui->data = dev;
1351 whiteout_ui->data_len = ubifs_encode_dev(dev, MKDEV(0, 0));
1352 ubifs_assert(c, !whiteout_ui->dirty);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001353
1354 memset(&wht_req, 0, sizeof(struct ubifs_budget_req));
1355 wht_req.dirtied_ino = 1;
1356 wht_req.dirtied_ino_d = ALIGN(whiteout_ui->data_len, 8);
1357 /*
1358 * To avoid deadlock between space budget (holds ui_mutex and
1359 * waits wb work) and writeback work(waits ui_mutex), do space
1360 * budget before ubifs inodes locked.
1361 */
1362 err = ubifs_budget_space(c, &wht_req);
1363 if (err) {
1364 iput(whiteout);
1365 goto out_release;
1366 }
1367
1368 /* Add the old_dentry size to the old_dir size. */
1369 old_sz -= CALC_DENT_SIZE(fname_len(&old_nm));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001370 }
1371
1372 lock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1373
1374 /*
1375 * Like most other Unix systems, set the @i_ctime for inodes on a
1376 * rename.
1377 */
1378 time = current_time(old_dir);
1379 old_inode->i_ctime = time;
1380
1381 /* We must adjust parent link count when renaming directories */
1382 if (is_dir) {
1383 if (move) {
1384 /*
1385 * @old_dir loses a link because we are moving
1386 * @old_inode to a different directory.
1387 */
1388 drop_nlink(old_dir);
1389 /*
1390 * @new_dir only gains a link if we are not also
1391 * overwriting an existing directory.
1392 */
1393 if (!unlink)
1394 inc_nlink(new_dir);
1395 } else {
1396 /*
1397 * @old_inode is not moving to a different directory,
1398 * but @old_dir still loses a link if we are
1399 * overwriting an existing directory.
1400 */
1401 if (unlink)
1402 drop_nlink(old_dir);
1403 }
1404 }
1405
1406 old_dir->i_size -= old_sz;
1407 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1408 old_dir->i_mtime = old_dir->i_ctime = time;
1409 new_dir->i_mtime = new_dir->i_ctime = time;
1410
1411 /*
1412 * And finally, if we unlinked a direntry which happened to have the
1413 * same name as the moved direntry, we have to decrement @i_nlink of
1414 * the unlinked inode and change its ctime.
1415 */
1416 if (unlink) {
1417 /*
1418 * Directories cannot have hard-links, so if this is a
1419 * directory, just clear @i_nlink.
1420 */
1421 saved_nlink = new_inode->i_nlink;
1422 if (is_dir)
1423 clear_nlink(new_inode);
1424 else
1425 drop_nlink(new_inode);
1426 new_inode->i_ctime = time;
1427 } else {
1428 new_dir->i_size += new_sz;
1429 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1430 }
1431
1432 /*
1433 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1434 * is dirty, because this will be done later on at the end of
1435 * 'ubifs_rename()'.
1436 */
1437 if (IS_SYNC(old_inode)) {
1438 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1439 if (unlink && IS_SYNC(new_inode))
1440 sync = 1;
1441 }
1442
1443 if (whiteout) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001444 inc_nlink(whiteout);
1445 mark_inode_dirty(whiteout);
Olivier Deprez0e641232021-09-23 10:07:05 +02001446
1447 spin_lock(&whiteout->i_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001448 whiteout->i_state &= ~I_LINKABLE;
Olivier Deprez0e641232021-09-23 10:07:05 +02001449 spin_unlock(&whiteout->i_lock);
1450
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001451 iput(whiteout);
1452 }
1453
1454 err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir,
1455 new_inode, &new_nm, whiteout, sync);
1456 if (err)
1457 goto out_cancel;
1458
1459 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1460 ubifs_release_budget(c, &req);
1461
1462 mutex_lock(&old_inode_ui->ui_mutex);
1463 release = old_inode_ui->dirty;
1464 mark_inode_dirty_sync(old_inode);
1465 mutex_unlock(&old_inode_ui->ui_mutex);
1466
1467 if (release)
1468 ubifs_release_budget(c, &ino_req);
1469 if (IS_SYNC(old_inode))
1470 err = old_inode->i_sb->s_op->write_inode(old_inode, NULL);
1471
1472 fscrypt_free_filename(&old_nm);
1473 fscrypt_free_filename(&new_nm);
1474 return err;
1475
1476out_cancel:
1477 if (unlink) {
1478 set_nlink(new_inode, saved_nlink);
1479 } else {
1480 new_dir->i_size -= new_sz;
1481 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1482 }
1483 old_dir->i_size += old_sz;
1484 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1485 if (is_dir) {
1486 if (move) {
1487 inc_nlink(old_dir);
1488 if (!unlink)
1489 drop_nlink(new_dir);
1490 } else {
1491 if (unlink)
1492 inc_nlink(old_dir);
1493 }
1494 }
1495 if (whiteout) {
1496 drop_nlink(whiteout);
1497 iput(whiteout);
1498 }
1499 unlock_4_inodes(old_dir, new_dir, new_inode, whiteout);
1500out_release:
1501 ubifs_release_budget(c, &ino_req);
1502 ubifs_release_budget(c, &req);
1503 fscrypt_free_filename(&old_nm);
1504 fscrypt_free_filename(&new_nm);
1505 return err;
1506}
1507
1508static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
1509 struct inode *new_dir, struct dentry *new_dentry)
1510{
1511 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1512 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1513 .dirtied_ino = 2 };
1514 int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1515 struct inode *fst_inode = d_inode(old_dentry);
1516 struct inode *snd_inode = d_inode(new_dentry);
1517 struct timespec64 time;
1518 int err;
1519 struct fscrypt_name fst_nm, snd_nm;
1520
1521 ubifs_assert(c, fst_inode && snd_inode);
1522
1523 err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &fst_nm);
1524 if (err)
1525 return err;
1526
1527 err = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &snd_nm);
1528 if (err) {
1529 fscrypt_free_filename(&fst_nm);
1530 return err;
1531 }
1532
1533 lock_4_inodes(old_dir, new_dir, NULL, NULL);
1534
1535 time = current_time(old_dir);
1536 fst_inode->i_ctime = time;
1537 snd_inode->i_ctime = time;
1538 old_dir->i_mtime = old_dir->i_ctime = time;
1539 new_dir->i_mtime = new_dir->i_ctime = time;
1540
1541 if (old_dir != new_dir) {
1542 if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
1543 inc_nlink(new_dir);
1544 drop_nlink(old_dir);
1545 }
1546 else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
1547 drop_nlink(new_dir);
1548 inc_nlink(old_dir);
1549 }
1550 }
1551
1552 err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir,
1553 snd_inode, &snd_nm, sync);
1554
1555 unlock_4_inodes(old_dir, new_dir, NULL, NULL);
1556 ubifs_release_budget(c, &req);
1557
1558 fscrypt_free_filename(&fst_nm);
1559 fscrypt_free_filename(&snd_nm);
1560 return err;
1561}
1562
1563static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
1564 struct inode *new_dir, struct dentry *new_dentry,
1565 unsigned int flags)
1566{
1567 int err;
1568 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1569
1570 if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
1571 return -EINVAL;
1572
1573 ubifs_assert(c, inode_is_locked(old_dir));
1574 ubifs_assert(c, inode_is_locked(new_dir));
1575
1576 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1577 flags);
1578 if (err)
1579 return err;
1580
1581 if (flags & RENAME_EXCHANGE)
1582 return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
1583
1584 return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1585}
1586
1587int ubifs_getattr(const struct path *path, struct kstat *stat,
1588 u32 request_mask, unsigned int flags)
1589{
1590 loff_t size;
1591 struct inode *inode = d_inode(path->dentry);
1592 struct ubifs_inode *ui = ubifs_inode(inode);
1593
1594 mutex_lock(&ui->ui_mutex);
1595
1596 if (ui->flags & UBIFS_APPEND_FL)
1597 stat->attributes |= STATX_ATTR_APPEND;
1598 if (ui->flags & UBIFS_COMPR_FL)
1599 stat->attributes |= STATX_ATTR_COMPRESSED;
1600 if (ui->flags & UBIFS_CRYPT_FL)
1601 stat->attributes |= STATX_ATTR_ENCRYPTED;
1602 if (ui->flags & UBIFS_IMMUTABLE_FL)
1603 stat->attributes |= STATX_ATTR_IMMUTABLE;
1604
1605 stat->attributes_mask |= (STATX_ATTR_APPEND |
1606 STATX_ATTR_COMPRESSED |
1607 STATX_ATTR_ENCRYPTED |
1608 STATX_ATTR_IMMUTABLE);
1609
1610 generic_fillattr(inode, stat);
1611 stat->blksize = UBIFS_BLOCK_SIZE;
1612 stat->size = ui->ui_size;
1613
1614 /*
1615 * Unfortunately, the 'stat()' system call was designed for block
1616 * device based file systems, and it is not appropriate for UBIFS,
1617 * because UBIFS does not have notion of "block". For example, it is
1618 * difficult to tell how many block a directory takes - it actually
1619 * takes less than 300 bytes, but we have to round it to block size,
1620 * which introduces large mistake. This makes utilities like 'du' to
1621 * report completely senseless numbers. This is the reason why UBIFS
1622 * goes the same way as JFFS2 - it reports zero blocks for everything
1623 * but regular files, which makes more sense than reporting completely
1624 * wrong sizes.
1625 */
1626 if (S_ISREG(inode->i_mode)) {
1627 size = ui->xattr_size;
1628 size += stat->size;
1629 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1630 /*
1631 * Note, user-space expects 512-byte blocks count irrespectively
1632 * of what was reported in @stat->size.
1633 */
1634 stat->blocks = size >> 9;
1635 } else
1636 stat->blocks = 0;
1637 mutex_unlock(&ui->ui_mutex);
1638 return 0;
1639}
1640
1641static int ubifs_dir_open(struct inode *dir, struct file *file)
1642{
Olivier Deprez157378f2022-04-04 15:47:50 +02001643 if (IS_ENCRYPTED(dir))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001644 return fscrypt_get_encryption_info(dir) ? -EACCES : 0;
1645
1646 return 0;
1647}
1648
1649const struct inode_operations ubifs_dir_inode_operations = {
1650 .lookup = ubifs_lookup,
1651 .create = ubifs_create,
1652 .link = ubifs_link,
1653 .symlink = ubifs_symlink,
1654 .unlink = ubifs_unlink,
1655 .mkdir = ubifs_mkdir,
1656 .rmdir = ubifs_rmdir,
1657 .mknod = ubifs_mknod,
1658 .rename = ubifs_rename,
1659 .setattr = ubifs_setattr,
1660 .getattr = ubifs_getattr,
1661#ifdef CONFIG_UBIFS_FS_XATTR
1662 .listxattr = ubifs_listxattr,
1663#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001664 .update_time = ubifs_update_time,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001665 .tmpfile = ubifs_tmpfile,
1666};
1667
1668const struct file_operations ubifs_dir_operations = {
1669 .llseek = generic_file_llseek,
1670 .release = ubifs_dir_release,
1671 .read = generic_read_dir,
1672 .iterate_shared = ubifs_readdir,
1673 .fsync = ubifs_fsync,
1674 .unlocked_ioctl = ubifs_ioctl,
1675 .open = ubifs_dir_open,
1676#ifdef CONFIG_COMPAT
1677 .compat_ioctl = ubifs_compat_ioctl,
1678#endif
1679};