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