Commit | Line | Data |
---|---|---|
1e51764a AB |
1 | /* * This file is part of UBIFS. |
2 | * | |
3 | * Copyright (C) 2006-2008 Nokia Corporation. | |
4 | * Copyright (C) 2006, 2007 University of Szeged, Hungary | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License version 2 as published by | |
8 | * the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | * | |
19 | * Authors: Artem Bityutskiy (Битюцкий Артём) | |
20 | * Adrian Hunter | |
21 | * Zoltan Sogor | |
22 | */ | |
23 | ||
24 | /* | |
25 | * This file implements directory operations. | |
26 | * | |
27 | * All FS operations in this file allocate budget before writing anything to the | |
28 | * media. If they fail to allocate it, the error is returned. The only | |
29 | * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even | |
30 | * if they unable to allocate the budget, because deletion %-ENOSPC failure is | |
31 | * not what users are usually ready to get. UBIFS budgeting subsystem has some | |
32 | * space reserved for these purposes. | |
33 | * | |
34 | * All operations in this file write all inodes which they change straight | |
35 | * away, instead of marking them dirty. For example, 'ubifs_link()' changes | |
36 | * @i_size of the parent inode and writes the parent inode together with the | |
37 | * target inode. This was done to simplify file-system recovery which would | |
38 | * otherwise be very difficult to do. The only exception is rename which marks | |
39 | * the re-named inode dirty (because its @i_ctime is updated) but does not | |
40 | * write it, but just marks it as dirty. | |
41 | */ | |
42 | ||
43 | #include "ubifs.h" | |
44 | ||
45 | /** | |
46 | * inherit_flags - inherit flags of the parent inode. | |
47 | * @dir: parent inode | |
48 | * @mode: new inode mode flags | |
49 | * | |
50 | * This is a helper function for 'ubifs_new_inode()' which inherits flag of the | |
51 | * parent directory inode @dir. UBIFS inodes inherit the following flags: | |
52 | * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on | |
53 | * sub-directory basis; | |
54 | * o %UBIFS_SYNC_FL - useful for the same reasons; | |
55 | * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories. | |
56 | * | |
57 | * This function returns the inherited flags. | |
58 | */ | |
ad44be5c | 59 | static int inherit_flags(const struct inode *dir, umode_t mode) |
1e51764a AB |
60 | { |
61 | int flags; | |
62 | const struct ubifs_inode *ui = ubifs_inode(dir); | |
63 | ||
64 | if (!S_ISDIR(dir->i_mode)) | |
65 | /* | |
66 | * The parent is not a directory, which means that an extended | |
67 | * attribute inode is being created. No flags. | |
68 | */ | |
69 | return 0; | |
70 | ||
71 | flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL); | |
72 | if (!S_ISDIR(mode)) | |
73 | /* The "DIRSYNC" flag only applies to directories */ | |
74 | flags &= ~UBIFS_DIRSYNC_FL; | |
75 | return flags; | |
76 | } | |
77 | ||
78 | /** | |
79 | * ubifs_new_inode - allocate new UBIFS inode object. | |
80 | * @c: UBIFS file-system description object | |
81 | * @dir: parent directory inode | |
82 | * @mode: inode mode flags | |
83 | * | |
84 | * This function finds an unused inode number, allocates new inode and | |
85 | * initializes it. Returns new inode in case of success and an error code in | |
86 | * case of failure. | |
87 | */ | |
88 | struct inode *ubifs_new_inode(struct ubifs_info *c, const struct inode *dir, | |
ad44be5c | 89 | umode_t mode) |
1e51764a AB |
90 | { |
91 | struct inode *inode; | |
92 | struct ubifs_inode *ui; | |
93 | ||
94 | inode = new_inode(c->vfs_sb); | |
95 | ui = ubifs_inode(inode); | |
96 | if (!inode) | |
97 | return ERR_PTR(-ENOMEM); | |
98 | ||
99 | /* | |
100 | * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and | |
101 | * marking them dirty in file write path (see 'file_update_time()'). | |
102 | * UBIFS has to fully control "clean <-> dirty" transitions of inodes | |
103 | * to make budgeting work. | |
104 | */ | |
12e776a0 | 105 | inode->i_flags |= S_NOCMTIME; |
1e51764a | 106 | |
abf5d08a | 107 | inode_init_owner(inode, dir, mode); |
1e51764a AB |
108 | inode->i_mtime = inode->i_atime = inode->i_ctime = |
109 | ubifs_current_time(inode); | |
110 | inode->i_mapping->nrpages = 0; | |
111 | /* Disable readahead */ | |
112 | inode->i_mapping->backing_dev_info = &c->bdi; | |
113 | ||
114 | switch (mode & S_IFMT) { | |
115 | case S_IFREG: | |
116 | inode->i_mapping->a_ops = &ubifs_file_address_operations; | |
117 | inode->i_op = &ubifs_file_inode_operations; | |
118 | inode->i_fop = &ubifs_file_operations; | |
119 | break; | |
120 | case S_IFDIR: | |
121 | inode->i_op = &ubifs_dir_inode_operations; | |
122 | inode->i_fop = &ubifs_dir_operations; | |
123 | inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ; | |
124 | break; | |
125 | case S_IFLNK: | |
126 | inode->i_op = &ubifs_symlink_inode_operations; | |
127 | break; | |
128 | case S_IFSOCK: | |
129 | case S_IFIFO: | |
130 | case S_IFBLK: | |
131 | case S_IFCHR: | |
132 | inode->i_op = &ubifs_file_inode_operations; | |
133 | break; | |
134 | default: | |
135 | BUG(); | |
136 | } | |
137 | ||
138 | ui->flags = inherit_flags(dir, mode); | |
139 | ubifs_set_inode_flags(inode); | |
140 | if (S_ISREG(mode)) | |
141 | ui->compr_type = c->default_compr; | |
142 | else | |
143 | ui->compr_type = UBIFS_COMPR_NONE; | |
144 | ui->synced_i_size = 0; | |
145 | ||
146 | spin_lock(&c->cnt_lock); | |
147 | /* Inode number overflow is currently not supported */ | |
148 | if (c->highest_inum >= INUM_WARN_WATERMARK) { | |
149 | if (c->highest_inum >= INUM_WATERMARK) { | |
150 | spin_unlock(&c->cnt_lock); | |
151 | ubifs_err("out of inode numbers"); | |
152 | make_bad_inode(inode); | |
153 | iput(inode); | |
154 | return ERR_PTR(-EINVAL); | |
155 | } | |
156 | ubifs_warn("running out of inode numbers (current %lu, max %d)", | |
e84461ad | 157 | (unsigned long)c->highest_inum, INUM_WATERMARK); |
1e51764a AB |
158 | } |
159 | ||
160 | inode->i_ino = ++c->highest_inum; | |
1e51764a AB |
161 | /* |
162 | * The creation sequence number remains with this inode for its | |
163 | * lifetime. All nodes for this inode have a greater sequence number, | |
164 | * and so it is possible to distinguish obsolete nodes belonging to a | |
165 | * previous incarnation of the same inode number - for example, for the | |
166 | * purpose of rebuilding the index. | |
167 | */ | |
168 | ui->creat_sqnum = ++c->max_sqnum; | |
169 | spin_unlock(&c->cnt_lock); | |
170 | return inode; | |
171 | } | |
172 | ||
bb2615d4 AB |
173 | static int dbg_check_name(const struct ubifs_info *c, |
174 | const struct ubifs_dent_node *dent, | |
175 | const struct qstr *nm) | |
1e51764a | 176 | { |
2b1844a8 | 177 | if (!dbg_is_chk_gen(c)) |
1e51764a AB |
178 | return 0; |
179 | if (le16_to_cpu(dent->nlen) != nm->len) | |
180 | return -EINVAL; | |
181 | if (memcmp(dent->name, nm->name, nm->len)) | |
182 | return -EINVAL; | |
183 | return 0; | |
184 | } | |
185 | ||
1e51764a | 186 | static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 187 | unsigned int flags) |
1e51764a AB |
188 | { |
189 | int err; | |
190 | union ubifs_key key; | |
191 | struct inode *inode = NULL; | |
192 | struct ubifs_dent_node *dent; | |
193 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
194 | ||
195 | dbg_gen("'%.*s' in dir ino %lu", | |
196 | dentry->d_name.len, dentry->d_name.name, dir->i_ino); | |
197 | ||
198 | if (dentry->d_name.len > UBIFS_MAX_NLEN) | |
199 | return ERR_PTR(-ENAMETOOLONG); | |
200 | ||
201 | dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); | |
202 | if (!dent) | |
203 | return ERR_PTR(-ENOMEM); | |
204 | ||
205 | dent_key_init(c, &key, dir->i_ino, &dentry->d_name); | |
206 | ||
207 | err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name); | |
208 | if (err) { | |
720b499c | 209 | if (err == -ENOENT) { |
1e51764a AB |
210 | dbg_gen("not found"); |
211 | goto done; | |
212 | } | |
213 | goto out; | |
214 | } | |
215 | ||
bb2615d4 | 216 | if (dbg_check_name(c, dent, &dentry->d_name)) { |
1e51764a AB |
217 | err = -EINVAL; |
218 | goto out; | |
219 | } | |
220 | ||
221 | inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum)); | |
222 | if (IS_ERR(inode)) { | |
223 | /* | |
224 | * This should not happen. Probably the file-system needs | |
225 | * checking. | |
226 | */ | |
227 | err = PTR_ERR(inode); | |
228 | ubifs_err("dead directory entry '%.*s', error %d", | |
229 | dentry->d_name.len, dentry->d_name.name, err); | |
230 | ubifs_ro_mode(c, err); | |
231 | goto out; | |
232 | } | |
233 | ||
234 | done: | |
235 | kfree(dent); | |
236 | /* | |
237 | * Note, d_splice_alias() would be required instead if we supported | |
238 | * NFS. | |
239 | */ | |
240 | d_add(dentry, inode); | |
241 | return NULL; | |
242 | ||
243 | out: | |
244 | kfree(dent); | |
245 | return ERR_PTR(err); | |
246 | } | |
247 | ||
4acdaf27 | 248 | static int ubifs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
ebfc3b49 | 249 | bool excl) |
1e51764a AB |
250 | { |
251 | struct inode *inode; | |
252 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
253 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
254 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
255 | .dirtied_ino = 1 }; | |
256 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
257 | ||
258 | /* | |
259 | * Budget request settings: new inode, new direntry, changing the | |
260 | * parent directory inode. | |
261 | */ | |
262 | ||
4acdaf27 | 263 | dbg_gen("dent '%.*s', mode %#hx in dir ino %lu", |
1e51764a AB |
264 | dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino); |
265 | ||
266 | err = ubifs_budget_space(c, &req); | |
267 | if (err) | |
268 | return err; | |
269 | ||
270 | inode = ubifs_new_inode(c, dir, mode); | |
271 | if (IS_ERR(inode)) { | |
272 | err = PTR_ERR(inode); | |
273 | goto out_budg; | |
274 | } | |
275 | ||
6fa3eb70 S |
276 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
277 | if (err) { | |
278 | ubifs_err("cannot initialize extended attribute, error %d", err); | |
279 | goto out_cancel; | |
280 | } | |
281 | ||
1e51764a AB |
282 | mutex_lock(&dir_ui->ui_mutex); |
283 | dir->i_size += sz_change; | |
284 | dir_ui->ui_size = dir->i_size; | |
285 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
286 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
287 | if (err) | |
288 | goto out_cancel; | |
6fa3eb70 | 289 | |
1e51764a AB |
290 | mutex_unlock(&dir_ui->ui_mutex); |
291 | ||
292 | ubifs_release_budget(c, &req); | |
293 | insert_inode_hash(inode); | |
294 | d_instantiate(dentry, inode); | |
295 | return 0; | |
296 | ||
297 | out_cancel: | |
298 | dir->i_size -= sz_change; | |
299 | dir_ui->ui_size = dir->i_size; | |
300 | mutex_unlock(&dir_ui->ui_mutex); | |
301 | make_bad_inode(inode); | |
302 | iput(inode); | |
303 | out_budg: | |
304 | ubifs_release_budget(c, &req); | |
305 | ubifs_err("cannot create regular file, error %d", err); | |
306 | return err; | |
307 | } | |
308 | ||
309 | /** | |
310 | * vfs_dent_type - get VFS directory entry type. | |
311 | * @type: UBIFS directory entry type | |
312 | * | |
313 | * This function converts UBIFS directory entry type into VFS directory entry | |
314 | * type. | |
315 | */ | |
316 | static unsigned int vfs_dent_type(uint8_t type) | |
317 | { | |
318 | switch (type) { | |
319 | case UBIFS_ITYPE_REG: | |
320 | return DT_REG; | |
321 | case UBIFS_ITYPE_DIR: | |
322 | return DT_DIR; | |
323 | case UBIFS_ITYPE_LNK: | |
324 | return DT_LNK; | |
325 | case UBIFS_ITYPE_BLK: | |
326 | return DT_BLK; | |
327 | case UBIFS_ITYPE_CHR: | |
328 | return DT_CHR; | |
329 | case UBIFS_ITYPE_FIFO: | |
330 | return DT_FIFO; | |
331 | case UBIFS_ITYPE_SOCK: | |
332 | return DT_SOCK; | |
333 | default: | |
334 | BUG(); | |
335 | } | |
336 | return 0; | |
337 | } | |
338 | ||
339 | /* | |
340 | * The classical Unix view for directory is that it is a linear array of | |
341 | * (name, inode number) entries. Linux/VFS assumes this model as well. | |
342 | * Particularly, 'readdir()' call wants us to return a directory entry offset | |
343 | * which later may be used to continue 'readdir()'ing the directory or to | |
344 | * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this | |
345 | * model because directory entries are identified by keys, which may collide. | |
346 | * | |
347 | * UBIFS uses directory entry hash value for directory offsets, so | |
348 | * 'seekdir()'/'telldir()' may not always work because of possible key | |
349 | * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work | |
350 | * properly by means of saving full directory entry name in the private field | |
351 | * of the file description object. | |
352 | * | |
353 | * This means that UBIFS cannot support NFS which requires full | |
354 | * 'seekdir()'/'telldir()' support. | |
355 | */ | |
356 | static int ubifs_readdir(struct file *file, void *dirent, filldir_t filldir) | |
357 | { | |
358 | int err, over = 0; | |
33f1a63a | 359 | loff_t pos = file->f_pos; |
1e51764a AB |
360 | struct qstr nm; |
361 | union ubifs_key key; | |
362 | struct ubifs_dent_node *dent; | |
496ad9aa | 363 | struct inode *dir = file_inode(file); |
1e51764a AB |
364 | struct ubifs_info *c = dir->i_sb->s_fs_info; |
365 | ||
33f1a63a | 366 | dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, pos); |
1e51764a | 367 | |
33f1a63a | 368 | if (pos > UBIFS_S_KEY_HASH_MASK || pos == 2) |
1e51764a AB |
369 | /* |
370 | * The directory was seek'ed to a senseless position or there | |
371 | * are no more entries. | |
372 | */ | |
373 | return 0; | |
374 | ||
605c912b AB |
375 | if (file->f_version == 0) { |
376 | /* | |
377 | * The file was seek'ed, which means that @file->private_data | |
378 | * is now invalid. This may also be just the first | |
379 | * 'ubifs_readdir()' invocation, in which case | |
380 | * @file->private_data is NULL, and the below code is | |
381 | * basically a no-op. | |
382 | */ | |
383 | kfree(file->private_data); | |
384 | file->private_data = NULL; | |
385 | } | |
386 | ||
387 | /* | |
388 | * 'generic_file_llseek()' unconditionally sets @file->f_version to | |
389 | * zero, and we use this for detecting whether the file was seek'ed. | |
390 | */ | |
391 | file->f_version = 1; | |
392 | ||
1e51764a | 393 | /* File positions 0 and 1 correspond to "." and ".." */ |
33f1a63a | 394 | if (pos == 0) { |
1e51764a AB |
395 | ubifs_assert(!file->private_data); |
396 | over = filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR); | |
397 | if (over) | |
398 | return 0; | |
33f1a63a | 399 | file->f_pos = pos = 1; |
1e51764a AB |
400 | } |
401 | ||
33f1a63a | 402 | if (pos == 1) { |
1e51764a AB |
403 | ubifs_assert(!file->private_data); |
404 | over = filldir(dirent, "..", 2, 1, | |
405 | parent_ino(file->f_path.dentry), DT_DIR); | |
406 | if (over) | |
407 | return 0; | |
408 | ||
409 | /* Find the first entry in TNC and save it */ | |
410 | lowest_dent_key(c, &key, dir->i_ino); | |
411 | nm.name = NULL; | |
412 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
413 | if (IS_ERR(dent)) { | |
414 | err = PTR_ERR(dent); | |
415 | goto out; | |
416 | } | |
417 | ||
33f1a63a | 418 | file->f_pos = pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
419 | file->private_data = dent; |
420 | } | |
421 | ||
422 | dent = file->private_data; | |
423 | if (!dent) { | |
424 | /* | |
425 | * The directory was seek'ed to and is now readdir'ed. | |
33f1a63a | 426 | * Find the entry corresponding to @pos or the closest one. |
1e51764a | 427 | */ |
33f1a63a | 428 | dent_key_init_hash(c, &key, dir->i_ino, pos); |
1e51764a AB |
429 | nm.name = NULL; |
430 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
431 | if (IS_ERR(dent)) { | |
432 | err = PTR_ERR(dent); | |
433 | goto out; | |
434 | } | |
33f1a63a | 435 | file->f_pos = pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
436 | file->private_data = dent; |
437 | } | |
438 | ||
439 | while (1) { | |
440 | dbg_gen("feed '%s', ino %llu, new f_pos %#x", | |
7424bac8 | 441 | dent->name, (unsigned long long)le64_to_cpu(dent->inum), |
1e51764a | 442 | key_hash_flash(c, &dent->key)); |
0ecb9529 HH |
443 | ubifs_assert(le64_to_cpu(dent->ch.sqnum) > |
444 | ubifs_inode(dir)->creat_sqnum); | |
1e51764a AB |
445 | |
446 | nm.len = le16_to_cpu(dent->nlen); | |
33f1a63a | 447 | over = filldir(dirent, dent->name, nm.len, pos, |
1e51764a AB |
448 | le64_to_cpu(dent->inum), |
449 | vfs_dent_type(dent->type)); | |
450 | if (over) | |
451 | return 0; | |
452 | ||
453 | /* Switch to the next entry */ | |
454 | key_read(c, &dent->key, &key); | |
455 | nm.name = dent->name; | |
456 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
457 | if (IS_ERR(dent)) { | |
458 | err = PTR_ERR(dent); | |
459 | goto out; | |
460 | } | |
461 | ||
462 | kfree(file->private_data); | |
33f1a63a | 463 | file->f_pos = pos = key_hash_flash(c, &dent->key); |
1e51764a AB |
464 | file->private_data = dent; |
465 | cond_resched(); | |
605c912b AB |
466 | |
467 | if (file->f_version == 0) | |
468 | /* | |
469 | * The file was seek'ed meanwhile, lets return and start | |
470 | * reading direntries from the new position on the next | |
471 | * invocation. | |
472 | */ | |
473 | return 0; | |
1e51764a AB |
474 | } |
475 | ||
476 | out: | |
477 | if (err != -ENOENT) { | |
478 | ubifs_err("cannot find next direntry, error %d", err); | |
479 | return err; | |
480 | } | |
481 | ||
482 | kfree(file->private_data); | |
483 | file->private_data = NULL; | |
605c912b | 484 | /* 2 is a special value indicating that there are no more direntries */ |
1e51764a AB |
485 | file->f_pos = 2; |
486 | return 0; | |
487 | } | |
488 | ||
965c8e59 | 489 | static loff_t ubifs_dir_llseek(struct file *file, loff_t offset, int whence) |
1e51764a | 490 | { |
965c8e59 | 491 | return generic_file_llseek(file, offset, whence); |
1e51764a AB |
492 | } |
493 | ||
494 | /* Free saved readdir() state when the directory is closed */ | |
495 | static int ubifs_dir_release(struct inode *dir, struct file *file) | |
496 | { | |
497 | kfree(file->private_data); | |
498 | file->private_data = NULL; | |
499 | return 0; | |
500 | } | |
501 | ||
502 | /** | |
82c1593c | 503 | * lock_2_inodes - a wrapper for locking two UBIFS inodes. |
1e51764a AB |
504 | * @inode1: first inode |
505 | * @inode2: second inode | |
82c1593c AB |
506 | * |
507 | * We do not implement any tricks to guarantee strict lock ordering, because | |
508 | * VFS has already done it for us on the @i_mutex. So this is just a simple | |
509 | * wrapper function. | |
1e51764a AB |
510 | */ |
511 | static void lock_2_inodes(struct inode *inode1, struct inode *inode2) | |
512 | { | |
82c1593c AB |
513 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
514 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); | |
1e51764a AB |
515 | } |
516 | ||
517 | /** | |
82c1593c | 518 | * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes. |
1e51764a AB |
519 | * @inode1: first inode |
520 | * @inode2: second inode | |
521 | */ | |
522 | static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) | |
523 | { | |
1e51764a | 524 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
82c1593c | 525 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); |
1e51764a AB |
526 | } |
527 | ||
528 | static int ubifs_link(struct dentry *old_dentry, struct inode *dir, | |
529 | struct dentry *dentry) | |
530 | { | |
531 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
532 | struct inode *inode = old_dentry->d_inode; | |
533 | struct ubifs_inode *ui = ubifs_inode(inode); | |
534 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
535 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
536 | struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2, | |
dab4b4d2 | 537 | .dirtied_ino_d = ALIGN(ui->data_len, 8) }; |
1e51764a AB |
538 | |
539 | /* | |
540 | * Budget request settings: new direntry, changing the target inode, | |
541 | * changing the parent inode. | |
542 | */ | |
543 | ||
544 | dbg_gen("dent '%.*s' to ino %lu (nlink %d) in dir ino %lu", | |
545 | dentry->d_name.len, dentry->d_name.name, inode->i_ino, | |
546 | inode->i_nlink, dir->i_ino); | |
82c1593c AB |
547 | ubifs_assert(mutex_is_locked(&dir->i_mutex)); |
548 | ubifs_assert(mutex_is_locked(&inode->i_mutex)); | |
8b3884a8 | 549 | |
d808efb4 | 550 | err = dbg_check_synced_i_size(c, inode); |
1e51764a AB |
551 | if (err) |
552 | return err; | |
553 | ||
554 | err = ubifs_budget_space(c, &req); | |
555 | if (err) | |
556 | return err; | |
557 | ||
558 | lock_2_inodes(dir, inode); | |
559 | inc_nlink(inode); | |
7de9c6ee | 560 | ihold(inode); |
1e51764a AB |
561 | inode->i_ctime = ubifs_current_time(inode); |
562 | dir->i_size += sz_change; | |
563 | dir_ui->ui_size = dir->i_size; | |
564 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
565 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
566 | if (err) | |
567 | goto out_cancel; | |
568 | unlock_2_inodes(dir, inode); | |
569 | ||
570 | ubifs_release_budget(c, &req); | |
571 | d_instantiate(dentry, inode); | |
572 | return 0; | |
573 | ||
574 | out_cancel: | |
575 | dir->i_size -= sz_change; | |
576 | dir_ui->ui_size = dir->i_size; | |
577 | drop_nlink(inode); | |
578 | unlock_2_inodes(dir, inode); | |
579 | ubifs_release_budget(c, &req); | |
580 | iput(inode); | |
581 | return err; | |
582 | } | |
583 | ||
584 | static int ubifs_unlink(struct inode *dir, struct dentry *dentry) | |
585 | { | |
586 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
587 | struct inode *inode = dentry->d_inode; | |
588 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
589 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
590 | int err, budgeted = 1; | |
591 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; | |
c43be108 | 592 | unsigned int saved_nlink = inode->i_nlink; |
6fa3eb70 S |
593 | /* MTK start |
594 | * add log to delete xattr | |
595 | */ | |
596 | union ubifs_key key; | |
597 | struct qstr nm = { .name = NULL }; | |
598 | ||
599 | lowest_xent_key(c, &key, inode->i_ino); | |
600 | while(1){ | |
601 | struct ubifs_dent_node *xent; | |
602 | ||
603 | xent = ubifs_tnc_next_ent(c, &key, &nm); | |
604 | if (IS_ERR(xent)) { | |
605 | err = PTR_ERR(xent); | |
606 | //ubifs_err("err %d\n", err); | |
607 | break; | |
608 | } | |
609 | //ubifs_err("remove xattr %s\n", xent->name); | |
610 | ubifs_removexattr(dentry, xent->name); | |
611 | kfree(xent); | |
612 | } | |
613 | /* MTK end */ | |
1e51764a AB |
614 | |
615 | /* | |
616 | * Budget request settings: deletion direntry, deletion inode (+1 for | |
617 | * @dirtied_ino), changing the parent directory inode. If budgeting | |
618 | * fails, go ahead anyway because we have extra space reserved for | |
619 | * deletions. | |
620 | */ | |
621 | ||
622 | dbg_gen("dent '%.*s' from ino %lu (nlink %d) in dir ino %lu", | |
623 | dentry->d_name.len, dentry->d_name.name, inode->i_ino, | |
624 | inode->i_nlink, dir->i_ino); | |
82c1593c AB |
625 | ubifs_assert(mutex_is_locked(&dir->i_mutex)); |
626 | ubifs_assert(mutex_is_locked(&inode->i_mutex)); | |
d808efb4 | 627 | err = dbg_check_synced_i_size(c, inode); |
1e51764a AB |
628 | if (err) |
629 | return err; | |
630 | ||
631 | err = ubifs_budget_space(c, &req); | |
632 | if (err) { | |
633 | if (err != -ENOSPC) | |
634 | return err; | |
1e51764a AB |
635 | budgeted = 0; |
636 | } | |
637 | ||
638 | lock_2_inodes(dir, inode); | |
639 | inode->i_ctime = ubifs_current_time(dir); | |
640 | drop_nlink(inode); | |
641 | dir->i_size -= sz_change; | |
642 | dir_ui->ui_size = dir->i_size; | |
643 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
644 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0); | |
645 | if (err) | |
646 | goto out_cancel; | |
647 | unlock_2_inodes(dir, inode); | |
648 | ||
649 | if (budgeted) | |
650 | ubifs_release_budget(c, &req); | |
651 | else { | |
652 | /* We've deleted something - clean the "no space" flags */ | |
b137545c | 653 | c->bi.nospace = c->bi.nospace_rp = 0; |
1e51764a AB |
654 | smp_wmb(); |
655 | } | |
656 | return 0; | |
657 | ||
658 | out_cancel: | |
659 | dir->i_size += sz_change; | |
660 | dir_ui->ui_size = dir->i_size; | |
c43be108 | 661 | set_nlink(inode, saved_nlink); |
1e51764a AB |
662 | unlock_2_inodes(dir, inode); |
663 | if (budgeted) | |
664 | ubifs_release_budget(c, &req); | |
665 | return err; | |
666 | } | |
667 | ||
668 | /** | |
669 | * check_dir_empty - check if a directory is empty or not. | |
670 | * @c: UBIFS file-system description object | |
671 | * @dir: VFS inode object of the directory to check | |
672 | * | |
673 | * This function checks if directory @dir is empty. Returns zero if the | |
674 | * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes | |
675 | * in case of of errors. | |
676 | */ | |
677 | static int check_dir_empty(struct ubifs_info *c, struct inode *dir) | |
678 | { | |
679 | struct qstr nm = { .name = NULL }; | |
680 | struct ubifs_dent_node *dent; | |
681 | union ubifs_key key; | |
682 | int err; | |
683 | ||
684 | lowest_dent_key(c, &key, dir->i_ino); | |
685 | dent = ubifs_tnc_next_ent(c, &key, &nm); | |
686 | if (IS_ERR(dent)) { | |
687 | err = PTR_ERR(dent); | |
688 | if (err == -ENOENT) | |
689 | err = 0; | |
690 | } else { | |
691 | kfree(dent); | |
692 | err = -ENOTEMPTY; | |
693 | } | |
694 | return err; | |
695 | } | |
696 | ||
697 | static int ubifs_rmdir(struct inode *dir, struct dentry *dentry) | |
698 | { | |
699 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
700 | struct inode *inode = dentry->d_inode; | |
701 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
702 | int err, budgeted = 1; | |
703 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
704 | struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 }; | |
705 | ||
706 | /* | |
707 | * Budget request settings: deletion direntry, deletion inode and | |
708 | * changing the parent inode. If budgeting fails, go ahead anyway | |
709 | * because we have extra space reserved for deletions. | |
710 | */ | |
711 | ||
712 | dbg_gen("directory '%.*s', ino %lu in dir ino %lu", dentry->d_name.len, | |
713 | dentry->d_name.name, inode->i_ino, dir->i_ino); | |
82c1593c AB |
714 | ubifs_assert(mutex_is_locked(&dir->i_mutex)); |
715 | ubifs_assert(mutex_is_locked(&inode->i_mutex)); | |
1e51764a AB |
716 | err = check_dir_empty(c, dentry->d_inode); |
717 | if (err) | |
718 | return err; | |
719 | ||
720 | err = ubifs_budget_space(c, &req); | |
721 | if (err) { | |
722 | if (err != -ENOSPC) | |
723 | return err; | |
724 | budgeted = 0; | |
725 | } | |
726 | ||
727 | lock_2_inodes(dir, inode); | |
728 | inode->i_ctime = ubifs_current_time(dir); | |
729 | clear_nlink(inode); | |
730 | drop_nlink(dir); | |
731 | dir->i_size -= sz_change; | |
732 | dir_ui->ui_size = dir->i_size; | |
733 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
734 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0); | |
735 | if (err) | |
736 | goto out_cancel; | |
737 | unlock_2_inodes(dir, inode); | |
738 | ||
739 | if (budgeted) | |
740 | ubifs_release_budget(c, &req); | |
741 | else { | |
742 | /* We've deleted something - clean the "no space" flags */ | |
b137545c | 743 | c->bi.nospace = c->bi.nospace_rp = 0; |
1e51764a AB |
744 | smp_wmb(); |
745 | } | |
746 | return 0; | |
747 | ||
748 | out_cancel: | |
749 | dir->i_size += sz_change; | |
750 | dir_ui->ui_size = dir->i_size; | |
751 | inc_nlink(dir); | |
c43be108 | 752 | set_nlink(inode, 2); |
1e51764a AB |
753 | unlock_2_inodes(dir, inode); |
754 | if (budgeted) | |
755 | ubifs_release_budget(c, &req); | |
756 | return err; | |
757 | } | |
758 | ||
18bb1db3 | 759 | static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1e51764a AB |
760 | { |
761 | struct inode *inode; | |
762 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
763 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
764 | int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
182854b4 | 765 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1 }; |
1e51764a AB |
766 | |
767 | /* | |
768 | * Budget request settings: new inode, new direntry and changing parent | |
769 | * directory inode. | |
770 | */ | |
771 | ||
18bb1db3 | 772 | dbg_gen("dent '%.*s', mode %#hx in dir ino %lu", |
1e51764a AB |
773 | dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino); |
774 | ||
775 | err = ubifs_budget_space(c, &req); | |
776 | if (err) | |
777 | return err; | |
778 | ||
779 | inode = ubifs_new_inode(c, dir, S_IFDIR | mode); | |
780 | if (IS_ERR(inode)) { | |
781 | err = PTR_ERR(inode); | |
782 | goto out_budg; | |
783 | } | |
784 | ||
6fa3eb70 S |
785 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
786 | if (err) { | |
787 | ubifs_err("cannot initialize extended attribute, error %d", | |
788 | err); | |
789 | goto out_cancel; | |
790 | } | |
791 | ||
1e51764a AB |
792 | mutex_lock(&dir_ui->ui_mutex); |
793 | insert_inode_hash(inode); | |
794 | inc_nlink(inode); | |
795 | inc_nlink(dir); | |
796 | dir->i_size += sz_change; | |
797 | dir_ui->ui_size = dir->i_size; | |
798 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
799 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
800 | if (err) { | |
801 | ubifs_err("cannot create directory, error %d", err); | |
802 | goto out_cancel; | |
803 | } | |
6fa3eb70 | 804 | |
1e51764a AB |
805 | mutex_unlock(&dir_ui->ui_mutex); |
806 | ||
807 | ubifs_release_budget(c, &req); | |
808 | d_instantiate(dentry, inode); | |
809 | return 0; | |
810 | ||
811 | out_cancel: | |
812 | dir->i_size -= sz_change; | |
813 | dir_ui->ui_size = dir->i_size; | |
814 | drop_nlink(dir); | |
815 | mutex_unlock(&dir_ui->ui_mutex); | |
816 | make_bad_inode(inode); | |
817 | iput(inode); | |
818 | out_budg: | |
819 | ubifs_release_budget(c, &req); | |
820 | return err; | |
821 | } | |
822 | ||
823 | static int ubifs_mknod(struct inode *dir, struct dentry *dentry, | |
1a67aafb | 824 | umode_t mode, dev_t rdev) |
1e51764a AB |
825 | { |
826 | struct inode *inode; | |
827 | struct ubifs_inode *ui; | |
828 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
829 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
830 | union ubifs_dev_desc *dev = NULL; | |
831 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
832 | int err, devlen = 0; | |
833 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
dab4b4d2 AB |
834 | .new_ino_d = ALIGN(devlen, 8), |
835 | .dirtied_ino = 1 }; | |
1e51764a AB |
836 | |
837 | /* | |
838 | * Budget request settings: new inode, new direntry and changing parent | |
839 | * directory inode. | |
840 | */ | |
841 | ||
842 | dbg_gen("dent '%.*s' in dir ino %lu", | |
843 | dentry->d_name.len, dentry->d_name.name, dir->i_ino); | |
844 | ||
845 | if (!new_valid_dev(rdev)) | |
846 | return -EINVAL; | |
847 | ||
848 | if (S_ISBLK(mode) || S_ISCHR(mode)) { | |
849 | dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); | |
850 | if (!dev) | |
851 | return -ENOMEM; | |
852 | devlen = ubifs_encode_dev(dev, rdev); | |
853 | } | |
854 | ||
855 | err = ubifs_budget_space(c, &req); | |
856 | if (err) { | |
857 | kfree(dev); | |
858 | return err; | |
859 | } | |
860 | ||
861 | inode = ubifs_new_inode(c, dir, mode); | |
862 | if (IS_ERR(inode)) { | |
863 | kfree(dev); | |
864 | err = PTR_ERR(inode); | |
865 | goto out_budg; | |
866 | } | |
867 | ||
868 | init_special_inode(inode, inode->i_mode, rdev); | |
869 | inode->i_size = ubifs_inode(inode)->ui_size = devlen; | |
870 | ui = ubifs_inode(inode); | |
871 | ui->data = dev; | |
872 | ui->data_len = devlen; | |
873 | ||
6fa3eb70 S |
874 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
875 | if (err) { | |
876 | ubifs_err("cannot initialize extended attribute, error %d", | |
877 | err); | |
878 | goto out_cancel; | |
879 | } | |
880 | ||
1e51764a AB |
881 | mutex_lock(&dir_ui->ui_mutex); |
882 | dir->i_size += sz_change; | |
883 | dir_ui->ui_size = dir->i_size; | |
884 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
885 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
886 | if (err) | |
887 | goto out_cancel; | |
6fa3eb70 | 888 | |
1e51764a AB |
889 | mutex_unlock(&dir_ui->ui_mutex); |
890 | ||
891 | ubifs_release_budget(c, &req); | |
892 | insert_inode_hash(inode); | |
893 | d_instantiate(dentry, inode); | |
894 | return 0; | |
895 | ||
896 | out_cancel: | |
897 | dir->i_size -= sz_change; | |
898 | dir_ui->ui_size = dir->i_size; | |
899 | mutex_unlock(&dir_ui->ui_mutex); | |
900 | make_bad_inode(inode); | |
901 | iput(inode); | |
902 | out_budg: | |
903 | ubifs_release_budget(c, &req); | |
904 | return err; | |
905 | } | |
906 | ||
907 | static int ubifs_symlink(struct inode *dir, struct dentry *dentry, | |
908 | const char *symname) | |
909 | { | |
910 | struct inode *inode; | |
911 | struct ubifs_inode *ui; | |
912 | struct ubifs_inode *dir_ui = ubifs_inode(dir); | |
913 | struct ubifs_info *c = dir->i_sb->s_fs_info; | |
914 | int err, len = strlen(symname); | |
915 | int sz_change = CALC_DENT_SIZE(dentry->d_name.len); | |
916 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, | |
dab4b4d2 AB |
917 | .new_ino_d = ALIGN(len, 8), |
918 | .dirtied_ino = 1 }; | |
1e51764a AB |
919 | |
920 | /* | |
921 | * Budget request settings: new inode, new direntry and changing parent | |
922 | * directory inode. | |
923 | */ | |
924 | ||
925 | dbg_gen("dent '%.*s', target '%s' in dir ino %lu", dentry->d_name.len, | |
926 | dentry->d_name.name, symname, dir->i_ino); | |
927 | ||
928 | if (len > UBIFS_MAX_INO_DATA) | |
929 | return -ENAMETOOLONG; | |
930 | ||
931 | err = ubifs_budget_space(c, &req); | |
932 | if (err) | |
933 | return err; | |
934 | ||
935 | inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO); | |
936 | if (IS_ERR(inode)) { | |
937 | err = PTR_ERR(inode); | |
938 | goto out_budg; | |
939 | } | |
940 | ||
941 | ui = ubifs_inode(inode); | |
942 | ui->data = kmalloc(len + 1, GFP_NOFS); | |
943 | if (!ui->data) { | |
944 | err = -ENOMEM; | |
945 | goto out_inode; | |
946 | } | |
947 | ||
948 | memcpy(ui->data, symname, len); | |
949 | ((char *)ui->data)[len] = '\0'; | |
950 | /* | |
951 | * The terminating zero byte is not written to the flash media and it | |
952 | * is put just to make later in-memory string processing simpler. Thus, | |
953 | * data length is @len, not @len + %1. | |
954 | */ | |
955 | ui->data_len = len; | |
956 | inode->i_size = ubifs_inode(inode)->ui_size = len; | |
957 | ||
6fa3eb70 S |
958 | err = ubifs_init_security(dir, inode, &dentry->d_name); |
959 | if (err) { | |
960 | ubifs_err("cannot initialize extended attribute, error %d", | |
961 | err); | |
962 | goto out_cancel; | |
963 | } | |
964 | ||
1e51764a AB |
965 | mutex_lock(&dir_ui->ui_mutex); |
966 | dir->i_size += sz_change; | |
967 | dir_ui->ui_size = dir->i_size; | |
968 | dir->i_mtime = dir->i_ctime = inode->i_ctime; | |
969 | err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0); | |
970 | if (err) | |
971 | goto out_cancel; | |
6fa3eb70 | 972 | |
1e51764a AB |
973 | mutex_unlock(&dir_ui->ui_mutex); |
974 | ||
975 | ubifs_release_budget(c, &req); | |
976 | insert_inode_hash(inode); | |
977 | d_instantiate(dentry, inode); | |
978 | return 0; | |
979 | ||
980 | out_cancel: | |
981 | dir->i_size -= sz_change; | |
982 | dir_ui->ui_size = dir->i_size; | |
983 | mutex_unlock(&dir_ui->ui_mutex); | |
984 | out_inode: | |
985 | make_bad_inode(inode); | |
986 | iput(inode); | |
987 | out_budg: | |
988 | ubifs_release_budget(c, &req); | |
989 | return err; | |
990 | } | |
991 | ||
992 | /** | |
82c1593c | 993 | * lock_3_inodes - a wrapper for locking three UBIFS inodes. |
1e51764a AB |
994 | * @inode1: first inode |
995 | * @inode2: second inode | |
996 | * @inode3: third inode | |
997 | * | |
82c1593c AB |
998 | * This function is used for 'ubifs_rename()' and @inode1 may be the same as |
999 | * @inode2 whereas @inode3 may be %NULL. | |
1000 | * | |
1001 | * We do not implement any tricks to guarantee strict lock ordering, because | |
1002 | * VFS has already done it for us on the @i_mutex. So this is just a simple | |
1003 | * wrapper function. | |
1e51764a AB |
1004 | */ |
1005 | static void lock_3_inodes(struct inode *inode1, struct inode *inode2, | |
1006 | struct inode *inode3) | |
1007 | { | |
82c1593c AB |
1008 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
1009 | if (inode2 != inode1) | |
1010 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); | |
1011 | if (inode3) | |
1012 | mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3); | |
1e51764a AB |
1013 | } |
1014 | ||
1015 | /** | |
82c1593c | 1016 | * unlock_3_inodes - a wrapper for unlocking three UBIFS inodes for rename. |
1e51764a AB |
1017 | * @inode1: first inode |
1018 | * @inode2: second inode | |
1019 | * @inode3: third inode | |
1020 | */ | |
1021 | static void unlock_3_inodes(struct inode *inode1, struct inode *inode2, | |
1022 | struct inode *inode3) | |
1023 | { | |
1e51764a AB |
1024 | if (inode3) |
1025 | mutex_unlock(&ubifs_inode(inode3)->ui_mutex); | |
82c1593c AB |
1026 | if (inode1 != inode2) |
1027 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); | |
1028 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); | |
1e51764a AB |
1029 | } |
1030 | ||
1031 | static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry, | |
1032 | struct inode *new_dir, struct dentry *new_dentry) | |
1033 | { | |
1034 | struct ubifs_info *c = old_dir->i_sb->s_fs_info; | |
1035 | struct inode *old_inode = old_dentry->d_inode; | |
1036 | struct inode *new_inode = new_dentry->d_inode; | |
1037 | struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode); | |
1038 | int err, release, sync = 0, move = (new_dir != old_dir); | |
1039 | int is_dir = S_ISDIR(old_inode->i_mode); | |
1040 | int unlink = !!new_inode; | |
1041 | int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len); | |
1042 | int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len); | |
1043 | struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1, | |
1044 | .dirtied_ino = 3 }; | |
1045 | struct ubifs_budget_req ino_req = { .dirtied_ino = 1, | |
dab4b4d2 | 1046 | .dirtied_ino_d = ALIGN(old_inode_ui->data_len, 8) }; |
1e51764a | 1047 | struct timespec time; |
782759b9 | 1048 | unsigned int uninitialized_var(saved_nlink); |
1e51764a AB |
1049 | |
1050 | /* | |
1051 | * Budget request settings: deletion direntry, new direntry, removing | |
1052 | * the old inode, and changing old and new parent directory inodes. | |
1053 | * | |
1054 | * However, this operation also marks the target inode as dirty and | |
1055 | * does not write it, so we allocate budget for the target inode | |
1056 | * separately. | |
1057 | */ | |
1058 | ||
79fda517 AB |
1059 | dbg_gen("dent '%.*s' ino %lu in dir ino %lu to dent '%.*s' in dir ino %lu", |
1060 | old_dentry->d_name.len, old_dentry->d_name.name, | |
1e51764a AB |
1061 | old_inode->i_ino, old_dir->i_ino, new_dentry->d_name.len, |
1062 | new_dentry->d_name.name, new_dir->i_ino); | |
82c1593c AB |
1063 | ubifs_assert(mutex_is_locked(&old_dir->i_mutex)); |
1064 | ubifs_assert(mutex_is_locked(&new_dir->i_mutex)); | |
1065 | if (unlink) | |
1066 | ubifs_assert(mutex_is_locked(&new_inode->i_mutex)); | |
1067 | ||
1e51764a AB |
1068 | |
1069 | if (unlink && is_dir) { | |
1070 | err = check_dir_empty(c, new_inode); | |
1071 | if (err) | |
1072 | return err; | |
1073 | } | |
1074 | ||
1075 | err = ubifs_budget_space(c, &req); | |
1076 | if (err) | |
1077 | return err; | |
1078 | err = ubifs_budget_space(c, &ino_req); | |
1079 | if (err) { | |
1080 | ubifs_release_budget(c, &req); | |
1081 | return err; | |
1082 | } | |
1083 | ||
1084 | lock_3_inodes(old_dir, new_dir, new_inode); | |
1085 | ||
1086 | /* | |
1087 | * Like most other Unix systems, set the @i_ctime for inodes on a | |
1088 | * rename. | |
1089 | */ | |
1090 | time = ubifs_current_time(old_dir); | |
1091 | old_inode->i_ctime = time; | |
1092 | ||
1093 | /* We must adjust parent link count when renaming directories */ | |
1094 | if (is_dir) { | |
1095 | if (move) { | |
1096 | /* | |
1097 | * @old_dir loses a link because we are moving | |
1098 | * @old_inode to a different directory. | |
1099 | */ | |
1100 | drop_nlink(old_dir); | |
1101 | /* | |
1102 | * @new_dir only gains a link if we are not also | |
1103 | * overwriting an existing directory. | |
1104 | */ | |
1105 | if (!unlink) | |
1106 | inc_nlink(new_dir); | |
1107 | } else { | |
1108 | /* | |
1109 | * @old_inode is not moving to a different directory, | |
1110 | * but @old_dir still loses a link if we are | |
1111 | * overwriting an existing directory. | |
1112 | */ | |
1113 | if (unlink) | |
1114 | drop_nlink(old_dir); | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | old_dir->i_size -= old_sz; | |
1119 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; | |
1120 | old_dir->i_mtime = old_dir->i_ctime = time; | |
1121 | new_dir->i_mtime = new_dir->i_ctime = time; | |
1122 | ||
1123 | /* | |
1124 | * And finally, if we unlinked a direntry which happened to have the | |
1125 | * same name as the moved direntry, we have to decrement @i_nlink of | |
1126 | * the unlinked inode and change its ctime. | |
1127 | */ | |
1128 | if (unlink) { | |
1129 | /* | |
1130 | * Directories cannot have hard-links, so if this is a | |
c43be108 | 1131 | * directory, just clear @i_nlink. |
1e51764a | 1132 | */ |
c43be108 | 1133 | saved_nlink = new_inode->i_nlink; |
1e51764a | 1134 | if (is_dir) |
c43be108 AB |
1135 | clear_nlink(new_inode); |
1136 | else | |
1e51764a AB |
1137 | drop_nlink(new_inode); |
1138 | new_inode->i_ctime = time; | |
1e51764a AB |
1139 | } else { |
1140 | new_dir->i_size += new_sz; | |
1141 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; | |
1142 | } | |
1143 | ||
1144 | /* | |
1145 | * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode | |
1146 | * is dirty, because this will be done later on at the end of | |
1147 | * 'ubifs_rename()'. | |
1148 | */ | |
1149 | if (IS_SYNC(old_inode)) { | |
1150 | sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir); | |
1151 | if (unlink && IS_SYNC(new_inode)) | |
1152 | sync = 1; | |
1153 | } | |
1154 | err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry, | |
1155 | sync); | |
1156 | if (err) | |
1157 | goto out_cancel; | |
1158 | ||
1159 | unlock_3_inodes(old_dir, new_dir, new_inode); | |
1160 | ubifs_release_budget(c, &req); | |
1161 | ||
1162 | mutex_lock(&old_inode_ui->ui_mutex); | |
1163 | release = old_inode_ui->dirty; | |
1164 | mark_inode_dirty_sync(old_inode); | |
1165 | mutex_unlock(&old_inode_ui->ui_mutex); | |
1166 | ||
1167 | if (release) | |
1168 | ubifs_release_budget(c, &ino_req); | |
1169 | if (IS_SYNC(old_inode)) | |
a9185b41 | 1170 | err = old_inode->i_sb->s_op->write_inode(old_inode, NULL); |
1e51764a AB |
1171 | return err; |
1172 | ||
1173 | out_cancel: | |
1174 | if (unlink) { | |
c43be108 | 1175 | set_nlink(new_inode, saved_nlink); |
1e51764a AB |
1176 | } else { |
1177 | new_dir->i_size -= new_sz; | |
1178 | ubifs_inode(new_dir)->ui_size = new_dir->i_size; | |
1179 | } | |
1180 | old_dir->i_size += old_sz; | |
1181 | ubifs_inode(old_dir)->ui_size = old_dir->i_size; | |
1182 | if (is_dir) { | |
1183 | if (move) { | |
1184 | inc_nlink(old_dir); | |
1185 | if (!unlink) | |
1186 | drop_nlink(new_dir); | |
1187 | } else { | |
1188 | if (unlink) | |
1189 | inc_nlink(old_dir); | |
1190 | } | |
1191 | } | |
1192 | unlock_3_inodes(old_dir, new_dir, new_inode); | |
1193 | ubifs_release_budget(c, &ino_req); | |
1194 | ubifs_release_budget(c, &req); | |
1195 | return err; | |
1196 | } | |
1197 | ||
1198 | int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry, | |
1199 | struct kstat *stat) | |
1200 | { | |
1201 | loff_t size; | |
1202 | struct inode *inode = dentry->d_inode; | |
1203 | struct ubifs_inode *ui = ubifs_inode(inode); | |
1204 | ||
1205 | mutex_lock(&ui->ui_mutex); | |
6d42e7e9 | 1206 | generic_fillattr(inode, stat); |
1e51764a AB |
1207 | stat->blksize = UBIFS_BLOCK_SIZE; |
1208 | stat->size = ui->ui_size; | |
1209 | ||
1210 | /* | |
1211 | * Unfortunately, the 'stat()' system call was designed for block | |
1212 | * device based file systems, and it is not appropriate for UBIFS, | |
1213 | * because UBIFS does not have notion of "block". For example, it is | |
1214 | * difficult to tell how many block a directory takes - it actually | |
1215 | * takes less than 300 bytes, but we have to round it to block size, | |
1216 | * which introduces large mistake. This makes utilities like 'du' to | |
1217 | * report completely senseless numbers. This is the reason why UBIFS | |
1218 | * goes the same way as JFFS2 - it reports zero blocks for everything | |
1219 | * but regular files, which makes more sense than reporting completely | |
1220 | * wrong sizes. | |
1221 | */ | |
1222 | if (S_ISREG(inode->i_mode)) { | |
1223 | size = ui->xattr_size; | |
1224 | size += stat->size; | |
1225 | size = ALIGN(size, UBIFS_BLOCK_SIZE); | |
1226 | /* | |
1227 | * Note, user-space expects 512-byte blocks count irrespectively | |
1228 | * of what was reported in @stat->size. | |
1229 | */ | |
1230 | stat->blocks = size >> 9; | |
1231 | } else | |
1232 | stat->blocks = 0; | |
1233 | mutex_unlock(&ui->ui_mutex); | |
1234 | return 0; | |
1235 | } | |
1236 | ||
e8b81566 | 1237 | const struct inode_operations ubifs_dir_inode_operations = { |
1e51764a AB |
1238 | .lookup = ubifs_lookup, |
1239 | .create = ubifs_create, | |
1240 | .link = ubifs_link, | |
1241 | .symlink = ubifs_symlink, | |
1242 | .unlink = ubifs_unlink, | |
1243 | .mkdir = ubifs_mkdir, | |
1244 | .rmdir = ubifs_rmdir, | |
1245 | .mknod = ubifs_mknod, | |
1246 | .rename = ubifs_rename, | |
1247 | .setattr = ubifs_setattr, | |
1248 | .getattr = ubifs_getattr, | |
1e51764a AB |
1249 | .setxattr = ubifs_setxattr, |
1250 | .getxattr = ubifs_getxattr, | |
1251 | .listxattr = ubifs_listxattr, | |
1252 | .removexattr = ubifs_removexattr, | |
1e51764a AB |
1253 | }; |
1254 | ||
e8b81566 | 1255 | const struct file_operations ubifs_dir_operations = { |
1e51764a AB |
1256 | .llseek = ubifs_dir_llseek, |
1257 | .release = ubifs_dir_release, | |
1258 | .read = generic_read_dir, | |
1259 | .readdir = ubifs_readdir, | |
1260 | .fsync = ubifs_fsync, | |
1261 | .unlocked_ioctl = ubifs_ioctl, | |
1262 | #ifdef CONFIG_COMPAT | |
1263 | .compat_ioctl = ubifs_compat_ioctl, | |
1264 | #endif | |
1265 | }; |