Commit | Line | Data |
---|---|---|
2b27bdcc | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1e51764a AB |
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 | * | |
1e51764a AB |
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 | */ | |
ad44be5c | 47 | static int inherit_flags(const struct inode *dir, umode_t mode) |
1e51764a AB |
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 | */ | |
d475a507 | 76 | struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir, |
ad44be5c | 77 | umode_t mode) |
1e51764a | 78 | { |
d475a507 | 79 | int err; |
1e51764a AB |
80 | struct inode *inode; |
81 | struct ubifs_inode *ui; | |
d475a507 RW |
82 | bool encrypted = false; |
83 | ||
1e51764a AB |
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 | */ | |
12e776a0 | 95 | inode->i_flags |= S_NOCMTIME; |
1e51764a | 96 | |
abf5d08a | 97 | inode_init_owner(inode, dir, mode); |
1e51764a | 98 | inode->i_mtime = inode->i_atime = inode->i_ctime = |
607a11ad | 99 | current_time(inode); |
1e51764a | 100 | inode->i_mapping->nrpages = 0; |
1e51764a | 101 | |
4c030fa8 EB |
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 | ||
1e51764a AB |
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); | |
235c362b | 145 | ubifs_err(c, "out of inode numbers"); |
4c030fa8 EB |
146 | err = -EINVAL; |
147 | goto out_iput; | |
1e51764a | 148 | } |
1a7e985d | 149 | ubifs_warn(c, "running out of inode numbers (current %lu, max %u)", |
e84461ad | 150 | (unsigned long)c->highest_inum, INUM_WATERMARK); |
1e51764a AB |
151 | } |
152 | ||
153 | inode->i_ino = ++c->highest_inum; | |
1e51764a AB |
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); | |
d475a507 RW |
163 | |
164 | if (encrypted) { | |
4c030fa8 | 165 | err = fscrypt_set_context(inode, NULL); |
d475a507 | 166 | if (err) { |
4c030fa8 EB |
167 | ubifs_err(c, "fscrypt_set_context failed: %i", err); |
168 | goto out_iput; | |
d475a507 RW |
169 | } |
170 | } | |
171 | ||
1e51764a | 172 | return inode; |
4c030fa8 EB |
173 | |
174 | out_iput: | |
175 | make_bad_inode(inode); | |
176 | iput(inode); | |
177 | return ERR_PTR(err); | |
1e51764a AB |
178 | } |
179 | ||
bb2615d4 AB |
180 | static int dbg_check_name(const struct ubifs_info *c, |
181 | const struct ubifs_dent_node *dent, | |
f4f61d2c | 182 | const struct fscrypt_name *nm) |
1e51764a | 183 | { |
2b1844a8 | 184 | if (!dbg_is_chk_gen(c)) |
1e51764a | 185 | return 0; |
f4f61d2c | 186 | if (le16_to_cpu(dent->nlen) != fname_len(nm)) |
1e51764a | 187 | return -EINVAL; |
f4f61d2c | 188 | if (memcmp(dent->name, fname_name(nm), fname_len(nm))) |
1e51764a AB |
189 | return -EINVAL; |
190 | return 0; | |
191 | } | |
192 | ||
1e51764a | 193 | static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 194 | unsigned int flags) |
1e51764a AB |
195 | { |
196 | int err; | |
197 | union ubifs_key key; | |
198 | struct inode *inode = NULL; | |
191ac107 | 199 | struct ubifs_dent_node *dent = NULL; |
1e51764a | 200 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
f4f61d2c | 201 | struct fscrypt_name nm; |
1e51764a | 202 | |
4cb2a01d | 203 | dbg_gen("'%pd' in dir ino %lu", dentry, dir->i_ino); |
1e51764a | 204 | |
b01531db EB |
205 | err = fscrypt_prepare_lookup(dir, dentry, &nm); |
206 | if (err == -ENOENT) | |
207 | return d_splice_alias(NULL, dentry); | |
f4f61d2c RW |
208 | if (err) |
209 | return ERR_PTR(err); | |
210 | ||
211 | if (fname_len(&nm) > UBIFS_MAX_NLEN) { | |
191ac107 AV |
212 | inode = ERR_PTR(-ENAMETOOLONG); |
213 | goto done; | |
f4f61d2c | 214 | } |
1e51764a AB |
215 | |
216 | dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); | |
f4f61d2c | 217 | if (!dent) { |
191ac107 AV |
218 | inode = ERR_PTR(-ENOMEM); |
219 | goto done; | |
f4f61d2c | 220 | } |
1e51764a | 221 | |
aec992aa | 222 | if (fname_name(&nm) == NULL) { |
f0d07a98 EB |
223 | if (nm.hash & ~UBIFS_S_KEY_HASH_MASK) |
224 | goto done; /* ENOENT */ | |
f4f61d2c | 225 | dent_key_init_hash(c, &key, dir->i_ino, nm.hash); |
528e3d17 | 226 | err = ubifs_tnc_lookup_dh(c, &key, dent, nm.minor_hash); |
f4f61d2c RW |
227 | } else { |
228 | dent_key_init(c, &key, dir->i_ino, &nm); | |
229 | err = ubifs_tnc_lookup_nm(c, &key, dent, &nm); | |
230 | } | |
1e51764a | 231 | |
1e51764a | 232 | if (err) { |
191ac107 | 233 | if (err == -ENOENT) |
1e51764a | 234 | dbg_gen("not found"); |
191ac107 AV |
235 | else |
236 | inode = ERR_PTR(err); | |
237 | goto done; | |
1e51764a AB |
238 | } |
239 | ||
f4f61d2c | 240 | if (dbg_check_name(c, dent, &nm)) { |
191ac107 AV |
241 | inode = ERR_PTR(-EINVAL); |
242 | goto done; | |
1e51764a AB |
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); | |
235c362b | 252 | ubifs_err(c, "dead directory entry '%pd', error %d", |
4cb2a01d | 253 | dentry, err); |
1e51764a | 254 | ubifs_ro_mode(c, err); |
191ac107 | 255 | goto done; |
1e51764a AB |
256 | } |
257 | ||
50d9fad7 | 258 | if (IS_ENCRYPTED(dir) && |
413d5a9e EB |
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); | |
191ac107 AV |
263 | iput(inode); |
264 | inode = ERR_PTR(-EPERM); | |
413d5a9e EB |
265 | } |
266 | ||
1e51764a AB |
267 | done: |
268 | kfree(dent); | |
f4f61d2c | 269 | fscrypt_free_filename(&nm); |
191ac107 | 270 | return d_splice_alias(inode, dentry); |
1e51764a AB |
271 | } |
272 | ||
4acdaf27 | 273 | static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
ebfc3b49 | 274 | bool excl) |
1e51764a AB |
275 | { |
276 | struct inode *inode; | |
277 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
1e51764a AB |
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); | |
f4f61d2c RW |
281 | struct fscrypt_name nm; |
282 | int err, sz_change; | |
1e51764a AB |
283 | |
284 | /* | |
285 | * Budget request settings: new inode, new direntry, changing the | |
286 | * parent directory inode. | |
287 | */ | |
288 | ||
4cb2a01d AV |
289 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
290 | dentry, mode, dir->i_ino); | |
1e51764a AB |
291 | |
292 | err = ubifs_budget_space(c, &req); | |
293 | if (err) | |
294 | return err; | |
295 | ||
f4f61d2c RW |
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 | ||
1e51764a AB |
302 | inode = ubifs_new_inode(c, dir, mode); |
303 | if (IS_ERR(inode)) { | |
304 | err = PTR_ERR(inode); | |
f4f61d2c | 305 | goto out_fname; |
1e51764a AB |
306 | } |
307 | ||
d7f0b70d SN |
308 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
309 | if (err) | |
9401a795 | 310 | goto out_inode; |
d7f0b70d | 311 | |
1e51764a AB |
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; | |
f4f61d2c | 316 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
1e51764a AB |
317 | if (err) |
318 | goto out_cancel; | |
319 | mutex_unlock(&dir_ui->ui_mutex); | |
320 | ||
321 | ubifs_release_budget(c, &req); | |
f4f61d2c | 322 | fscrypt_free_filename(&nm); |
1e51764a AB |
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); | |
9401a795 | 331 | out_inode: |
1e51764a AB |
332 | make_bad_inode(inode); |
333 | iput(inode); | |
f4f61d2c RW |
334 | out_fname: |
335 | fscrypt_free_filename(&nm); | |
1e51764a AB |
336 | out_budg: |
337 | ubifs_release_budget(c, &req); | |
235c362b | 338 | ubifs_err(c, "cannot create regular file, error %d", err); |
1e51764a AB |
339 | return err; |
340 | } | |
341 | ||
9e0a1fff RW |
342 | static int do_tmpfile(struct inode *dir, struct dentry *dentry, |
343 | umode_t mode, struct inode **whiteout) | |
474b9370 RW |
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; | |
f4f61d2c | 351 | struct fscrypt_name nm; |
474b9370 RW |
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 | ||
f4f61d2c | 361 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm); |
474b9370 RW |
362 | if (err) |
363 | return err; | |
364 | ||
f4f61d2c RW |
365 | err = ubifs_budget_space(c, &req); |
366 | if (err) { | |
367 | fscrypt_free_filename(&nm); | |
368 | return err; | |
369 | } | |
370 | ||
474b9370 RW |
371 | err = ubifs_budget_space(c, &ino_req); |
372 | if (err) { | |
373 | ubifs_release_budget(c, &req); | |
f4f61d2c | 374 | fscrypt_free_filename(&nm); |
474b9370 RW |
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 | ||
9e0a1fff RW |
385 | if (whiteout) { |
386 | init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); | |
6eb61d58 | 387 | ubifs_assert(c, inode->i_op == &ubifs_file_inode_operations); |
9e0a1fff RW |
388 | } |
389 | ||
474b9370 RW |
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); | |
9e0a1fff RW |
396 | |
397 | if (whiteout) { | |
398 | mark_inode_dirty(inode); | |
399 | drop_nlink(inode); | |
400 | *whiteout = inode; | |
401 | } else { | |
402 | d_tmpfile(dentry, inode); | |
403 | } | |
6eb61d58 | 404 | ubifs_assert(c, ui->dirty); |
9e0a1fff | 405 | |
474b9370 RW |
406 | instantiated = 1; |
407 | mutex_unlock(&ui->ui_mutex); | |
408 | ||
409 | mutex_lock(&dir_ui->ui_mutex); | |
f4f61d2c | 410 | err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0); |
474b9370 RW |
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); | |
f4f61d2c | 429 | fscrypt_free_filename(&nm); |
474b9370 RW |
430 | ubifs_err(c, "cannot create temporary file, error %d", err); |
431 | return err; | |
432 | } | |
433 | ||
9e0a1fff RW |
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 | ||
1e51764a AB |
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 | */ | |
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 | */ | |
01122e06 | 487 | static int ubifs_readdir(struct file *file, struct dir_context *ctx) |
1e51764a | 488 | { |
ba75d570 | 489 | int fstr_real_len = 0, err = 0; |
f4f61d2c RW |
490 | struct fscrypt_name nm; |
491 | struct fscrypt_str fstr = {0}; | |
1e51764a AB |
492 | union ubifs_key key; |
493 | struct ubifs_dent_node *dent; | |
496ad9aa | 494 | struct inode *dir = file_inode(file); |
1e51764a | 495 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
50d9fad7 | 496 | bool encrypted = IS_ENCRYPTED(dir); |
1e51764a | 497 | |
01122e06 | 498 | dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, ctx->pos); |
1e51764a | 499 | |
01122e06 | 500 | if (ctx->pos > UBIFS_S_KEY_HASH_MASK || ctx->pos == 2) |
1e51764a AB |
501 | /* |
502 | * The directory was seek'ed to a senseless position or there | |
503 | * are no more entries. | |
504 | */ | |
505 | return 0; | |
506 | ||
f4f61d2c RW |
507 | if (encrypted) { |
508 | err = fscrypt_get_encryption_info(dir); | |
3b1ada55 | 509 | if (err) |
f4f61d2c RW |
510 | return err; |
511 | ||
8b10fe68 | 512 | err = fscrypt_fname_alloc_buffer(UBIFS_MAX_NLEN, &fstr); |
f4f61d2c RW |
513 | if (err) |
514 | return err; | |
515 | ||
516 | fstr_real_len = fstr.len; | |
517 | } | |
518 | ||
605c912b AB |
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 | ||
1e51764a | 537 | /* File positions 0 and 1 correspond to "." and ".." */ |
01122e06 | 538 | if (ctx->pos < 2) { |
6eb61d58 | 539 | ubifs_assert(c, !file->private_data); |
f4f61d2c RW |
540 | if (!dir_emit_dots(file, ctx)) { |
541 | if (encrypted) | |
542 | fscrypt_fname_free_buffer(&fstr); | |
1e51764a | 543 | return 0; |
f4f61d2c | 544 | } |
1e51764a AB |
545 | |
546 | /* Find the first entry in TNC and save it */ | |
547 | lowest_dent_key(c, &key, dir->i_ino); | |
f4f61d2c | 548 | fname_len(&nm) = 0; |
1e51764a AB |
549 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
550 | if (IS_ERR(dent)) { | |
551 | err = PTR_ERR(dent); | |
552 | goto out; | |
553 | } | |
554 | ||
01122e06 | 555 | ctx->pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
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. | |
01122e06 | 563 | * Find the entry corresponding to @ctx->pos or the closest one. |
1e51764a | 564 | */ |
01122e06 | 565 | dent_key_init_hash(c, &key, dir->i_ino, ctx->pos); |
f4f61d2c | 566 | fname_len(&nm) = 0; |
1e51764a AB |
567 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
568 | if (IS_ERR(dent)) { | |
569 | err = PTR_ERR(dent); | |
570 | goto out; | |
571 | } | |
01122e06 | 572 | ctx->pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
573 | file->private_data = dent; |
574 | } | |
575 | ||
576 | while (1) { | |
b20e2d99 HL |
577 | dbg_gen("ino %llu, new f_pos %#x", |
578 | (unsigned long long)le64_to_cpu(dent->inum), | |
1e51764a | 579 | key_hash_flash(c, &dent->key)); |
6eb61d58 | 580 | ubifs_assert(c, le64_to_cpu(dent->ch.sqnum) > |
0ecb9529 | 581 | ubifs_inode(dir)->creat_sqnum); |
1e51764a | 582 | |
f4f61d2c RW |
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 | ||
528e3d17 RW |
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); | |
ca7f85be | 593 | if (err) |
f4f61d2c RW |
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, | |
1e51764a | 601 | le64_to_cpu(dent->inum), |
f4f61d2c RW |
602 | vfs_dent_type(dent->type))) { |
603 | if (encrypted) | |
604 | fscrypt_fname_free_buffer(&fstr); | |
1e51764a | 605 | return 0; |
f4f61d2c | 606 | } |
1e51764a AB |
607 | |
608 | /* Switch to the next entry */ | |
609 | key_read(c, &dent->key, &key); | |
1e51764a AB |
610 | dent = ubifs_tnc_next_ent(c, &key, &nm); |
611 | if (IS_ERR(dent)) { | |
612 | err = PTR_ERR(dent); | |
613 | goto out; | |
614 | } | |
615 | ||
616 | kfree(file->private_data); | |
01122e06 | 617 | ctx->pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
618 | file->private_data = dent; |
619 | cond_resched(); | |
620 | } | |
621 | ||
622 | out: | |
aeeb14f7 RW |
623 | kfree(file->private_data); |
624 | file->private_data = NULL; | |
625 | ||
f4f61d2c RW |
626 | if (encrypted) |
627 | fscrypt_fname_free_buffer(&fstr); | |
628 | ||
c83ed4c9 | 629 | if (err != -ENOENT) |
235c362b | 630 | ubifs_err(c, "cannot find next direntry, error %d", err); |
a00052a2 RW |
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 | ||
1e51764a | 639 | |
605c912b | 640 | /* 2 is a special value indicating that there are no more direntries */ |
01122e06 | 641 | ctx->pos = 2; |
c83ed4c9 | 642 | return err; |
1e51764a AB |
643 | } |
644 | ||
1e51764a AB |
645 | /* Free saved readdir() state when the directory is closed */ |
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 | /** | |
82c1593c | 654 | * lock_2_inodes - a wrapper for locking two UBIFS inodes. |
1e51764a AB |
655 | * @inode1: first inode |
656 | * @inode2: second inode | |
82c1593c AB |
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. | |
1e51764a AB |
661 | */ |
662 | static void lock_2_inodes(struct inode *inode1, struct inode *inode2) | |
663 | { | |
82c1593c AB |
664 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
665 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); | |
1e51764a AB |
666 | } |
667 | ||
668 | /** | |
82c1593c | 669 | * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes. |
1e51764a AB |
670 | * @inode1: first inode |
671 | * @inode2: second inode | |
672 | */ | |
673 | static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) | |
674 | { | |
1e51764a | 675 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
82c1593c | 676 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); |
1e51764a AB |
677 | } |
678 | ||
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; | |
2b0143b5 | 683 | struct inode *inode = d_inode(old_dentry); |
1e51764a AB |
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, | |
dab4b4d2 | 688 | .dirtied_ino_d = ALIGN(ui->data_len, 8) }; |
f4f61d2c | 689 | struct fscrypt_name nm; |
1e51764a AB |
690 | |
691 | /* | |
692 | * Budget request settings: new direntry, changing the target inode, | |
693 | * changing the parent inode. | |
694 | */ | |
695 | ||
4cb2a01d AV |
696 | dbg_gen("dent '%pd' to ino %lu (nlink %d) in dir ino %lu", |
697 | dentry, inode->i_ino, | |
1e51764a | 698 | inode->i_nlink, dir->i_ino); |
6eb61d58 RW |
699 | ubifs_assert(c, inode_is_locked(dir)); |
700 | ubifs_assert(c, inode_is_locked(inode)); | |
8b3884a8 | 701 | |
5653878c EB |
702 | err = fscrypt_prepare_link(old_dentry, dir, dentry); |
703 | if (err) | |
704 | return err; | |
f4f61d2c RW |
705 | |
706 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm); | |
1e51764a AB |
707 | if (err) |
708 | return err; | |
709 | ||
f4f61d2c RW |
710 | err = dbg_check_synced_i_size(c, inode); |
711 | if (err) | |
712 | goto out_fname; | |
713 | ||
1e51764a AB |
714 | err = ubifs_budget_space(c, &req); |
715 | if (err) | |
f4f61d2c | 716 | goto out_fname; |
1e51764a AB |
717 | |
718 | lock_2_inodes(dir, inode); | |
32fe905c RW |
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 | ||
1e51764a | 724 | inc_nlink(inode); |
7de9c6ee | 725 | ihold(inode); |
607a11ad | 726 | inode->i_ctime = current_time(inode); |
1e51764a AB |
727 | dir->i_size += sz_change; |
728 | dir_ui->ui_size = dir->i_size; | |
729 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
f4f61d2c | 730 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
1e51764a AB |
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); | |
f4f61d2c | 737 | fscrypt_free_filename(&nm); |
1e51764a AB |
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); | |
32fe905c RW |
744 | if (inode->i_nlink == 0) |
745 | ubifs_add_orphan(c, inode->i_ino); | |
1e51764a AB |
746 | unlock_2_inodes(dir, inode); |
747 | ubifs_release_budget(c, &req); | |
748 | iput(inode); | |
f4f61d2c RW |
749 | out_fname: |
750 | fscrypt_free_filename(&nm); | |
1e51764a AB |
751 | return err; |
752 | } | |
753 | ||
754 | static int ubifs_unlink(struct inode *dir, struct dentry *dentry) | |
755 | { | |
756 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
2b0143b5 | 757 | struct inode *inode = d_inode(dentry); |
1e51764a | 758 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
f4f61d2c | 759 | int err, sz_change, budgeted = 1; |
1e51764a | 760 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; |
c43be108 | 761 | unsigned int saved_nlink = inode->i_nlink; |
f4f61d2c | 762 | struct fscrypt_name nm; |
1e51764a AB |
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 | ||
4cb2a01d AV |
771 | dbg_gen("dent '%pd' from ino %lu (nlink %d) in dir ino %lu", |
772 | dentry, inode->i_ino, | |
1e51764a | 773 | inode->i_nlink, dir->i_ino); |
f4f61d2c | 774 | |
f4f61d2c RW |
775 | err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm); |
776 | if (err) | |
777 | return err; | |
778 | ||
9ca2d732 RW |
779 | err = ubifs_purge_xattrs(inode); |
780 | if (err) | |
781 | return err; | |
782 | ||
f4f61d2c RW |
783 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
784 | ||
6eb61d58 RW |
785 | ubifs_assert(c, inode_is_locked(dir)); |
786 | ubifs_assert(c, inode_is_locked(inode)); | |
d808efb4 | 787 | err = dbg_check_synced_i_size(c, inode); |
1e51764a | 788 | if (err) |
f4f61d2c | 789 | goto out_fname; |
1e51764a AB |
790 | |
791 | err = ubifs_budget_space(c, &req); | |
792 | if (err) { | |
793 | if (err != -ENOSPC) | |
f4f61d2c | 794 | goto out_fname; |
1e51764a AB |
795 | budgeted = 0; |
796 | } | |
797 | ||
798 | lock_2_inodes(dir, inode); | |
607a11ad | 799 | inode->i_ctime = current_time(dir); |
1e51764a AB |
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; | |
f4f61d2c | 804 | err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0); |
1e51764a AB |
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 */ | |
b137545c | 813 | c->bi.nospace = c->bi.nospace_rp = 0; |
1e51764a AB |
814 | smp_wmb(); |
815 | } | |
f4f61d2c | 816 | fscrypt_free_filename(&nm); |
1e51764a AB |
817 | return 0; |
818 | ||
819 | out_cancel: | |
820 | dir->i_size += sz_change; | |
821 | dir_ui->ui_size = dir->i_size; | |
c43be108 | 822 | set_nlink(inode, saved_nlink); |
1e51764a AB |
823 | unlock_2_inodes(dir, inode); |
824 | if (budgeted) | |
825 | ubifs_release_budget(c, &req); | |
f4f61d2c RW |
826 | out_fname: |
827 | fscrypt_free_filename(&nm); | |
1e51764a AB |
828 | return err; |
829 | } | |
830 | ||
831 | /** | |
832 | * check_dir_empty - check if a directory is empty or not. | |
1e51764a AB |
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 | */ | |
f6337d84 | 839 | int ubifs_check_dir_empty(struct inode *dir) |
1e51764a | 840 | { |
f6337d84 | 841 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
f4f61d2c | 842 | struct fscrypt_name nm = { 0 }; |
1e51764a AB |
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 | ||
860 | static int ubifs_rmdir(struct inode *dir, struct dentry *dentry) | |
861 | { | |
862 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
2b0143b5 | 863 | struct inode *inode = d_inode(dentry); |
f4f61d2c | 864 | int err, sz_change, budgeted = 1; |
1e51764a AB |
865 | struct ubifs_inode *dir_ui = ubifs_inode(dir); |
866 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; | |
f4f61d2c | 867 | struct fscrypt_name nm; |
1e51764a AB |
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 | ||
4cb2a01d AV |
875 | dbg_gen("directory '%pd', ino %lu in dir ino %lu", dentry, |
876 | inode->i_ino, dir->i_ino); | |
6eb61d58 RW |
877 | ubifs_assert(c, inode_is_locked(dir)); |
878 | ubifs_assert(c, inode_is_locked(inode)); | |
f6337d84 | 879 | err = ubifs_check_dir_empty(d_inode(dentry)); |
1e51764a AB |
880 | if (err) |
881 | return err; | |
882 | ||
f4f61d2c RW |
883 | err = fscrypt_setup_filename(dir, &dentry->d_name, 1, &nm); |
884 | if (err) | |
885 | return err; | |
886 | ||
9ca2d732 RW |
887 | err = ubifs_purge_xattrs(inode); |
888 | if (err) | |
889 | return err; | |
890 | ||
f4f61d2c RW |
891 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
892 | ||
1e51764a AB |
893 | err = ubifs_budget_space(c, &req); |
894 | if (err) { | |
895 | if (err != -ENOSPC) | |
f4f61d2c | 896 | goto out_fname; |
1e51764a AB |
897 | budgeted = 0; |
898 | } | |
899 | ||
900 | lock_2_inodes(dir, inode); | |
607a11ad | 901 | inode->i_ctime = current_time(dir); |
1e51764a AB |
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; | |
f4f61d2c | 907 | err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0); |
1e51764a AB |
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 */ | |
b137545c | 916 | c->bi.nospace = c->bi.nospace_rp = 0; |
1e51764a AB |
917 | smp_wmb(); |
918 | } | |
f4f61d2c | 919 | fscrypt_free_filename(&nm); |
1e51764a AB |
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); | |
c43be108 | 926 | set_nlink(inode, 2); |
1e51764a AB |
927 | unlock_2_inodes(dir, inode); |
928 | if (budgeted) | |
929 | ubifs_release_budget(c, &req); | |
f4f61d2c RW |
930 | out_fname: |
931 | fscrypt_free_filename(&nm); | |
1e51764a AB |
932 | return err; |
933 | } | |
934 | ||
18bb1db3 | 935 | static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1e51764a AB |
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; | |
f4f61d2c | 940 | int err, sz_change; |
182854b4 | 941 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 }; |
f4f61d2c | 942 | struct fscrypt_name nm; |
1e51764a AB |
943 | |
944 | /* | |
945 | * Budget request settings: new inode, new direntry and changing parent | |
946 | * directory inode. | |
947 | */ | |
948 | ||
4cb2a01d AV |
949 | dbg_gen("dent '%pd', mode %#hx in dir ino %lu", |
950 | dentry, mode, dir->i_ino); | |
1e51764a AB |
951 | |
952 | err = ubifs_budget_space(c, &req); | |
953 | if (err) | |
954 | return err; | |
955 | ||
f4f61d2c RW |
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 | ||
1e51764a AB |
962 | inode = ubifs_new_inode(c, dir, S_IFDIR | mode); |
963 | if (IS_ERR(inode)) { | |
964 | err = PTR_ERR(inode); | |
f4f61d2c | 965 | goto out_fname; |
1e51764a AB |
966 | } |
967 | ||
d7f0b70d SN |
968 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
969 | if (err) | |
9401a795 | 970 | goto out_inode; |
d7f0b70d | 971 | |
1e51764a AB |
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; | |
f4f61d2c | 979 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
1e51764a | 980 | if (err) { |
235c362b | 981 | ubifs_err(c, "cannot create directory, error %d", err); |
1e51764a AB |
982 | goto out_cancel; |
983 | } | |
984 | mutex_unlock(&dir_ui->ui_mutex); | |
985 | ||
986 | ubifs_release_budget(c, &req); | |
987 | d_instantiate(dentry, inode); | |
f4f61d2c | 988 | fscrypt_free_filename(&nm); |
1e51764a AB |
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); | |
9401a795 | 996 | out_inode: |
1e51764a AB |
997 | make_bad_inode(inode); |
998 | iput(inode); | |
f4f61d2c RW |
999 | out_fname: |
1000 | fscrypt_free_filename(&nm); | |
1e51764a AB |
1001 | out_budg: |
1002 | ubifs_release_budget(c, &req); | |
1003 | return err; | |
1004 | } | |
1005 | ||
1006 | static int ubifs_mknod(struct inode *dir, struct dentry *dentry, | |
1a67aafb | 1007 | umode_t mode, dev_t rdev) |
1e51764a AB |
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; | |
f4f61d2c | 1014 | int sz_change; |
1e51764a AB |
1015 | int err, devlen = 0; |
1016 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
dab4b4d2 | 1017 | .dirtied_ino = 1 }; |
f4f61d2c | 1018 | struct fscrypt_name nm; |
1e51764a AB |
1019 | |
1020 | /* | |
1021 | * Budget request settings: new inode, new direntry and changing parent | |
1022 | * directory inode. | |
1023 | */ | |
1024 | ||
4cb2a01d | 1025 | dbg_gen("dent '%pd' in dir ino %lu", dentry, dir->i_ino); |
1e51764a | 1026 | |
1e51764a AB |
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 | ||
4d35ca4f | 1034 | req.new_ino_d = ALIGN(devlen, 8); |
1e51764a AB |
1035 | err = ubifs_budget_space(c, &req); |
1036 | if (err) { | |
1037 | kfree(dev); | |
1038 | return err; | |
1039 | } | |
1040 | ||
f4f61d2c | 1041 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm); |
63ed6573 RW |
1042 | if (err) { |
1043 | kfree(dev); | |
f4f61d2c | 1044 | goto out_budg; |
63ed6573 | 1045 | } |
f4f61d2c RW |
1046 | |
1047 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); | |
1048 | ||
1e51764a AB |
1049 | inode = ubifs_new_inode(c, dir, mode); |
1050 | if (IS_ERR(inode)) { | |
1051 | kfree(dev); | |
1052 | err = PTR_ERR(inode); | |
f4f61d2c | 1053 | goto out_fname; |
1e51764a AB |
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 | ||
d7f0b70d SN |
1062 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
1063 | if (err) | |
9401a795 | 1064 | goto out_inode; |
d7f0b70d | 1065 | |
1e51764a AB |
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; | |
f4f61d2c | 1070 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
1e51764a AB |
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); | |
f4f61d2c | 1078 | fscrypt_free_filename(&nm); |
1e51764a AB |
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); | |
9401a795 | 1085 | out_inode: |
1e51764a AB |
1086 | make_bad_inode(inode); |
1087 | iput(inode); | |
f4f61d2c RW |
1088 | out_fname: |
1089 | fscrypt_free_filename(&nm); | |
1e51764a AB |
1090 | out_budg: |
1091 | ubifs_release_budget(c, &req); | |
1092 | return err; | |
1093 | } | |
1094 | ||
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; | |
00ee8b60 | 1102 | int err, sz_change, len = strlen(symname); |
0e4dda29 | 1103 | struct fscrypt_str disk_link; |
1e51764a | 1104 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, |
dab4b4d2 AB |
1105 | .new_ino_d = ALIGN(len, 8), |
1106 | .dirtied_ino = 1 }; | |
ca7f85be RW |
1107 | struct fscrypt_name nm; |
1108 | ||
0e4dda29 EB |
1109 | dbg_gen("dent '%pd', target '%s' in dir ino %lu", dentry, |
1110 | symname, dir->i_ino); | |
ca7f85be | 1111 | |
0e4dda29 EB |
1112 | err = fscrypt_prepare_symlink(dir, symname, len, UBIFS_MAX_INO_DATA, |
1113 | &disk_link); | |
1114 | if (err) | |
1115 | return err; | |
1e51764a AB |
1116 | |
1117 | /* | |
1118 | * Budget request settings: new inode, new direntry and changing parent | |
1119 | * directory inode. | |
1120 | */ | |
1e51764a AB |
1121 | err = ubifs_budget_space(c, &req); |
1122 | if (err) | |
1123 | return err; | |
1124 | ||
ca7f85be RW |
1125 | err = fscrypt_setup_filename(dir, &dentry->d_name, 0, &nm); |
1126 | if (err) | |
1127 | goto out_budg; | |
1128 | ||
00ee8b60 RW |
1129 | sz_change = CALC_DENT_SIZE(fname_len(&nm)); |
1130 | ||
1e51764a AB |
1131 | inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO); |
1132 | if (IS_ERR(inode)) { | |
1133 | err = PTR_ERR(inode); | |
ca7f85be | 1134 | goto out_fname; |
1e51764a AB |
1135 | } |
1136 | ||
1137 | ui = ubifs_inode(inode); | |
ca7f85be | 1138 | ui->data = kmalloc(disk_link.len, GFP_NOFS); |
1e51764a AB |
1139 | if (!ui->data) { |
1140 | err = -ENOMEM; | |
1141 | goto out_inode; | |
1142 | } | |
1143 | ||
0e4dda29 EB |
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); | |
6b46d444 | 1147 | if (err) |
ca7f85be | 1148 | goto out_inode; |
ca7f85be | 1149 | } else { |
0e4dda29 | 1150 | memcpy(ui->data, disk_link.name, disk_link.len); |
ca7f85be RW |
1151 | inode->i_link = ui->data; |
1152 | } | |
1153 | ||
1e51764a AB |
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, | |
0e4dda29 | 1157 | * data length is @disk_link.len - 1, not @disk_link.len. |
1e51764a | 1158 | */ |
ca7f85be RW |
1159 | ui->data_len = disk_link.len - 1; |
1160 | inode->i_size = ubifs_inode(inode)->ui_size = disk_link.len - 1; | |
1e51764a | 1161 | |
d7f0b70d SN |
1162 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
1163 | if (err) | |
9401a795 | 1164 | goto out_inode; |
d7f0b70d | 1165 | |
1e51764a AB |
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; | |
ca7f85be | 1170 | err = ubifs_jnl_update(c, dir, &nm, inode, 0, 0); |
1e51764a AB |
1171 | if (err) |
1172 | goto out_cancel; | |
1173 | mutex_unlock(&dir_ui->ui_mutex); | |
1174 | ||
1e51764a AB |
1175 | insert_inode_hash(inode); |
1176 | d_instantiate(dentry, inode); | |
6b46d444 EB |
1177 | err = 0; |
1178 | goto out_fname; | |
1e51764a AB |
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); | |
ca7f85be RW |
1187 | out_fname: |
1188 | fscrypt_free_filename(&nm); | |
1e51764a AB |
1189 | out_budg: |
1190 | ubifs_release_budget(c, &req); | |
1191 | return err; | |
1192 | } | |
1193 | ||
1194 | /** | |
9e0a1fff | 1195 | * lock_4_inodes - a wrapper for locking three UBIFS inodes. |
1e51764a AB |
1196 | * @inode1: first inode |
1197 | * @inode2: second inode | |
1198 | * @inode3: third inode | |
9e0a1fff | 1199 | * @inode4: fouth inode |
1e51764a | 1200 | * |
82c1593c | 1201 | * This function is used for 'ubifs_rename()' and @inode1 may be the same as |
9e0a1fff | 1202 | * @inode2 whereas @inode3 and @inode4 may be %NULL. |
82c1593c AB |
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. | |
1e51764a | 1207 | */ |
9e0a1fff RW |
1208 | static void lock_4_inodes(struct inode *inode1, struct inode *inode2, |
1209 | struct inode *inode3, struct inode *inode4) | |
1e51764a | 1210 | { |
82c1593c AB |
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); | |
9e0a1fff RW |
1216 | if (inode4) |
1217 | mutex_lock_nested(&ubifs_inode(inode4)->ui_mutex, WB_MUTEX_4); | |
1e51764a AB |
1218 | } |
1219 | ||
1220 | /** | |
9e0a1fff | 1221 | * unlock_4_inodes - a wrapper for unlocking three UBIFS inodes for rename. |
1e51764a AB |
1222 | * @inode1: first inode |
1223 | * @inode2: second inode | |
1224 | * @inode3: third inode | |
9e0a1fff | 1225 | * @inode4: fouth inode |
1e51764a | 1226 | */ |
9e0a1fff RW |
1227 | static void unlock_4_inodes(struct inode *inode1, struct inode *inode2, |
1228 | struct inode *inode3, struct inode *inode4) | |
1e51764a | 1229 | { |
9e0a1fff RW |
1230 | if (inode4) |
1231 | mutex_unlock(&ubifs_inode(inode4)->ui_mutex); | |
1e51764a AB |
1232 | if (inode3) |
1233 | mutex_unlock(&ubifs_inode(inode3)->ui_mutex); | |
82c1593c AB |
1234 | if (inode1 != inode2) |
1235 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); | |
1236 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); | |
1e51764a AB |
1237 | } |
1238 | ||
390975ac RW |
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) | |
1e51764a AB |
1242 | { |
1243 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; | |
2b0143b5 DH |
1244 | struct inode *old_inode = d_inode(old_dentry); |
1245 | struct inode *new_inode = d_inode(new_dentry); | |
9e0a1fff | 1246 | struct inode *whiteout = NULL; |
1e51764a | 1247 | struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode); |
9e0a1fff | 1248 | struct ubifs_inode *whiteout_ui = NULL; |
1e51764a AB |
1249 | int err, release, sync = 0, move = (new_dir != old_dir); |
1250 | int is_dir = S_ISDIR(old_inode->i_mode); | |
f4f61d2c | 1251 | int unlink = !!new_inode, new_sz, old_sz; |
1e51764a AB |
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, | |
dab4b4d2 | 1255 | .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) }; |
95582b00 | 1256 | struct timespec64 time; |
3f649ab7 | 1257 | unsigned int saved_nlink; |
f4f61d2c | 1258 | struct fscrypt_name old_nm, new_nm; |
1e51764a AB |
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 | ||
9e0a1fff | 1269 | dbg_gen("dent '%pd' ino %lu in dir ino %lu to dent '%pd' in dir ino %lu flags 0x%x", |
4cb2a01d | 1270 | old_dentry, old_inode->i_ino, old_dir->i_ino, |
9e0a1fff RW |
1271 | new_dentry, new_dir->i_ino, flags); |
1272 | ||
9ca2d732 | 1273 | if (unlink) { |
6eb61d58 | 1274 | ubifs_assert(c, inode_is_locked(new_inode)); |
82c1593c | 1275 | |
9ca2d732 RW |
1276 | err = ubifs_purge_xattrs(new_inode); |
1277 | if (err) | |
1278 | return err; | |
1279 | } | |
1280 | ||
1e51764a | 1281 | if (unlink && is_dir) { |
f6337d84 | 1282 | err = ubifs_check_dir_empty(new_inode); |
1e51764a AB |
1283 | if (err) |
1284 | return err; | |
1285 | } | |
1286 | ||
f4f61d2c | 1287 | err = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_nm); |
1e51764a AB |
1288 | if (err) |
1289 | return err; | |
f4f61d2c RW |
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 | } | |
1e51764a AB |
1306 | err = ubifs_budget_space(c, &ino_req); |
1307 | if (err) { | |
f4f61d2c RW |
1308 | fscrypt_free_filename(&old_nm); |
1309 | fscrypt_free_filename(&new_nm); | |
1e51764a AB |
1310 | ubifs_release_budget(c, &req); |
1311 | return err; | |
1312 | } | |
1313 | ||
9e0a1fff RW |
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) { | |
bb50c632 HL |
1319 | err = -ENOMEM; |
1320 | goto out_release; | |
9e0a1fff RW |
1321 | } |
1322 | ||
1323 | err = do_tmpfile(old_dir, old_dentry, S_IFCHR | WHITEOUT_MODE, &whiteout); | |
1324 | if (err) { | |
9e0a1fff | 1325 | kfree(dev); |
bb50c632 | 1326 | goto out_release; |
9e0a1fff RW |
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)); | |
6eb61d58 | 1333 | ubifs_assert(c, !whiteout_ui->dirty); |
9e0a1fff RW |
1334 | } |
1335 | ||
1336 | lock_4_inodes(old_dir, new_dir, new_inode, whiteout); | |
1e51764a AB |
1337 | |
1338 | /* | |
1339 | * Like most other Unix systems, set the @i_ctime for inodes on a | |
1340 | * rename. | |
1341 | */ | |
607a11ad | 1342 | time = current_time(old_dir); |
1e51764a AB |
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 | |
c43be108 | 1383 | * directory, just clear @i_nlink. |
1e51764a | 1384 | */ |
c43be108 | 1385 | saved_nlink = new_inode->i_nlink; |
1e51764a | 1386 | if (is_dir) |
c43be108 AB |
1387 | clear_nlink(new_inode); |
1388 | else | |
1e51764a AB |
1389 | drop_nlink(new_inode); |
1390 | new_inode->i_ctime = time; | |
1e51764a AB |
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 | } | |
9e0a1fff RW |
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) { | |
9e0a1fff RW |
1414 | kfree(whiteout_ui->data); |
1415 | whiteout_ui->data_len = 0; | |
1416 | iput(whiteout); | |
bb50c632 | 1417 | goto out_release; |
9e0a1fff RW |
1418 | } |
1419 | ||
1420 | inc_nlink(whiteout); | |
1421 | mark_inode_dirty(whiteout); | |
1422 | whiteout->i_state &= ~I_LINKABLE; | |
1423 | iput(whiteout); | |
1424 | } | |
1425 | ||
f4f61d2c RW |
1426 | err = ubifs_jnl_rename(c, old_dir, old_inode, &old_nm, new_dir, |
1427 | new_inode, &new_nm, whiteout, sync); | |
1e51764a AB |
1428 | if (err) |
1429 | goto out_cancel; | |
1430 | ||
9e0a1fff | 1431 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); |
1e51764a AB |
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)) | |
a9185b41 | 1442 | err = old_inode->i_sb->s_op->write_inode(old_inode, NULL); |
f4f61d2c RW |
1443 | |
1444 | fscrypt_free_filename(&old_nm); | |
1445 | fscrypt_free_filename(&new_nm); | |
1e51764a AB |
1446 | return err; |
1447 | ||
1448 | out_cancel: | |
1449 | if (unlink) { | |
c43be108 | 1450 | set_nlink(new_inode, saved_nlink); |
1e51764a AB |
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 | } | |
9e0a1fff RW |
1467 | if (whiteout) { |
1468 | drop_nlink(whiteout); | |
1469 | iput(whiteout); | |
1470 | } | |
1471 | unlock_4_inodes(old_dir, new_dir, new_inode, whiteout); | |
bb50c632 | 1472 | out_release: |
1e51764a AB |
1473 | ubifs_release_budget(c, &ino_req); |
1474 | ubifs_release_budget(c, &req); | |
f4f61d2c RW |
1475 | fscrypt_free_filename(&old_nm); |
1476 | fscrypt_free_filename(&new_nm); | |
1e51764a AB |
1477 | return err; |
1478 | } | |
1479 | ||
9ec64962 RW |
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); | |
95582b00 | 1489 | struct timespec64 time; |
9ec64962 | 1490 | int err; |
f4f61d2c | 1491 | struct fscrypt_name fst_nm, snd_nm; |
9ec64962 | 1492 | |
6eb61d58 | 1493 | ubifs_assert(c, fst_inode && snd_inode); |
9ec64962 | 1494 | |
f4f61d2c RW |
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 | ||
9ec64962 RW |
1505 | lock_4_inodes(old_dir, new_dir, NULL, NULL); |
1506 | ||
607a11ad | 1507 | time = current_time(old_dir); |
9ec64962 RW |
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 | ||
f4f61d2c RW |
1524 | err = ubifs_jnl_xrename(c, old_dir, fst_inode, &fst_nm, new_dir, |
1525 | snd_inode, &snd_nm, sync); | |
9ec64962 RW |
1526 | |
1527 | unlock_4_inodes(old_dir, new_dir, NULL, NULL); | |
1528 | ubifs_release_budget(c, &req); | |
1529 | ||
f4f61d2c RW |
1530 | fscrypt_free_filename(&fst_nm); |
1531 | fscrypt_free_filename(&snd_nm); | |
9ec64962 RW |
1532 | return err; |
1533 | } | |
1534 | ||
390975ac | 1535 | static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry, |
9ec64962 RW |
1536 | struct inode *new_dir, struct dentry *new_dentry, |
1537 | unsigned int flags) | |
1538 | { | |
0c1ad524 | 1539 | int err; |
6eb61d58 | 1540 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; |
0c1ad524 | 1541 | |
9ec64962 RW |
1542 | if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE)) |
1543 | return -EINVAL; | |
1544 | ||
6eb61d58 RW |
1545 | ubifs_assert(c, inode_is_locked(old_dir)); |
1546 | ubifs_assert(c, inode_is_locked(new_dir)); | |
9ec64962 | 1547 | |
0c1ad524 EB |
1548 | err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, |
1549 | flags); | |
1550 | if (err) | |
1551 | return err; | |
1552 | ||
9ec64962 RW |
1553 | if (flags & RENAME_EXCHANGE) |
1554 | return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry); | |
1555 | ||
390975ac | 1556 | return do_rename(old_dir, old_dentry, new_dir, new_dentry, flags); |
9ec64962 RW |
1557 | } |
1558 | ||
a528d35e DH |
1559 | int ubifs_getattr(const struct path *path, struct kstat *stat, |
1560 | u32 request_mask, unsigned int flags) | |
1e51764a AB |
1561 | { |
1562 | loff_t size; | |
a528d35e | 1563 | struct inode *inode = d_inode(path->dentry); |
1e51764a AB |
1564 | struct ubifs_inode *ui = ubifs_inode(inode); |
1565 | ||
1566 | mutex_lock(&ui->ui_mutex); | |
a02a6eba RW |
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 | ||
6d42e7e9 | 1582 | generic_fillattr(inode, stat); |
1e51764a AB |
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 | ||
ba40e6a3 RW |
1613 | static int ubifs_dir_open(struct inode *dir, struct file *file) |
1614 | { | |
50d9fad7 | 1615 | if (IS_ENCRYPTED(dir)) |
ba40e6a3 RW |
1616 | return fscrypt_get_encryption_info(dir) ? -EACCES : 0; |
1617 | ||
1618 | return 0; | |
1619 | } | |
1620 | ||
e8b81566 | 1621 | const struct inode_operations ubifs_dir_inode_operations = { |
1e51764a AB |
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, | |
390975ac | 1630 | .rename = ubifs_rename, |
1e51764a AB |
1631 | .setattr = ubifs_setattr, |
1632 | .getattr = ubifs_getattr, | |
7e5471ce | 1633 | #ifdef CONFIG_UBIFS_FS_XATTR |
1e51764a | 1634 | .listxattr = ubifs_listxattr, |
7e5471ce | 1635 | #endif |
8c1c5f26 | 1636 | .update_time = ubifs_update_time, |
474b9370 | 1637 | .tmpfile = ubifs_tmpfile, |
1e51764a AB |
1638 | }; |
1639 | ||
e8b81566 | 1640 | const struct file_operations ubifs_dir_operations = { |
01122e06 | 1641 | .llseek = generic_file_llseek, |
1e51764a AB |
1642 | .release = ubifs_dir_release, |
1643 | .read = generic_read_dir, | |
c51da20c | 1644 | .iterate_shared = ubifs_readdir, |
1e51764a AB |
1645 | .fsync = ubifs_fsync, |
1646 | .unlocked_ioctl = ubifs_ioctl, | |
ba40e6a3 | 1647 | .open = ubifs_dir_open, |
1e51764a AB |
1648 | #ifdef CONFIG_COMPAT |
1649 | .compat_ioctl = ubifs_compat_ioctl, | |
1650 | #endif | |
1651 | }; |