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