blob: 1110d56a23f5e7f58884d842b163dd0547c6e441 [file] [log] [blame]
Ryusuke Konishid2500652009-04-06 19:01:34 -07001/*
2 * namei.c - NILFS pathname lookup operations.
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Modified for NILFS by Amagai Yoshiji <amagai@osrg.net>,
21 * Ryusuke Konishi <ryusuke@osrg.net>
22 */
23/*
24 * linux/fs/ext2/namei.c
25 *
26 * Copyright (C) 1992, 1993, 1994, 1995
27 * Remy Card (card@masi.ibp.fr)
28 * Laboratoire MASI - Institut Blaise Pascal
29 * Universite Pierre et Marie Curie (Paris VI)
30 *
31 * from
32 *
33 * linux/fs/minix/namei.c
34 *
35 * Copyright (C) 1991, 1992 Linus Torvalds
36 *
37 * Big-endian to little-endian byte-swapping/bitmaps by
38 * David S. Miller (davem@caip.rutgers.edu), 1995
39 */
40
41#include <linux/pagemap.h>
42#include "nilfs.h"
43
44
45static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
46{
47 int err = nilfs_add_link(dentry, inode);
48 if (!err) {
49 d_instantiate(dentry, inode);
50 return 0;
51 }
52 inode_dec_link_count(inode);
53 iput(inode);
54 return err;
55}
56
57/*
58 * Methods themselves.
59 */
60
61static struct dentry *
62nilfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
63{
64 struct inode *inode;
65 ino_t ino;
66
67 if (dentry->d_name.len > NILFS_NAME_LEN)
68 return ERR_PTR(-ENAMETOOLONG);
69
Al Viro03190032010-01-31 21:02:09 -050070 ino = nilfs_inode_by_name(dir, &dentry->d_name);
Ryusuke Konishid2500652009-04-06 19:01:34 -070071 inode = NULL;
72 if (ino) {
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +090073 inode = nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino);
Ryusuke Konishid2500652009-04-06 19:01:34 -070074 if (IS_ERR(inode))
75 return ERR_CAST(inode);
76 }
77 return d_splice_alias(inode, dentry);
78}
79
80struct dentry *nilfs_get_parent(struct dentry *child)
81{
82 unsigned long ino;
83 struct inode *inode;
Al Viro03190032010-01-31 21:02:09 -050084 struct qstr dotdot = {.name = "..", .len = 2};
Ryusuke Konishid2500652009-04-06 19:01:34 -070085
86 ino = nilfs_inode_by_name(child->d_inode, &dotdot);
87 if (!ino)
88 return ERR_PTR(-ENOENT);
89
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +090090 inode = nilfs_iget(child->d_inode->i_sb,
91 NILFS_I(child->d_inode)->i_root, ino);
Ryusuke Konishid2500652009-04-06 19:01:34 -070092 if (IS_ERR(inode))
93 return ERR_CAST(inode);
94 return d_obtain_alias(inode);
95}
96
97/*
98 * By the time this is called, we already have created
99 * the directory cache entry for the new file, but it
100 * is so far negative - it has no inode.
101 *
102 * If the create succeeds, we fill in the inode information
103 * with d_instantiate().
104 */
105static int nilfs_create(struct inode *dir, struct dentry *dentry, int mode,
106 struct nameidata *nd)
107{
108 struct inode *inode;
109 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700110 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700111
112 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
113 if (err)
114 return err;
115 inode = nilfs_new_inode(dir, mode);
116 err = PTR_ERR(inode);
117 if (!IS_ERR(inode)) {
118 inode->i_op = &nilfs_file_inode_operations;
119 inode->i_fop = &nilfs_file_operations;
120 inode->i_mapping->a_ops = &nilfs_aops;
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900121 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700122 err = nilfs_add_nondir(dentry, inode);
123 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700124 if (!err)
125 err = nilfs_transaction_commit(dir->i_sb);
126 else
127 nilfs_transaction_abort(dir->i_sb);
128
129 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700130}
131
132static int
133nilfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
134{
135 struct inode *inode;
136 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700137 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700138
139 if (!new_valid_dev(rdev))
140 return -EINVAL;
141
142 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
143 if (err)
144 return err;
145 inode = nilfs_new_inode(dir, mode);
146 err = PTR_ERR(inode);
147 if (!IS_ERR(inode)) {
148 init_special_inode(inode, inode->i_mode, rdev);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900149 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700150 err = nilfs_add_nondir(dentry, inode);
151 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700152 if (!err)
153 err = nilfs_transaction_commit(dir->i_sb);
154 else
155 nilfs_transaction_abort(dir->i_sb);
156
157 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700158}
159
160static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
161 const char *symname)
162{
163 struct nilfs_transaction_info ti;
164 struct super_block *sb = dir->i_sb;
165 unsigned l = strlen(symname)+1;
166 struct inode *inode;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700167 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700168
169 if (l > sb->s_blocksize)
170 return -ENAMETOOLONG;
171
172 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
173 if (err)
174 return err;
175
176 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
177 err = PTR_ERR(inode);
178 if (IS_ERR(inode))
179 goto out;
180
181 /* slow symlink */
182 inode->i_op = &nilfs_symlink_inode_operations;
183 inode->i_mapping->a_ops = &nilfs_aops;
184 err = page_symlink(inode, symname, l);
185 if (err)
186 goto out_fail;
187
188 /* mark_inode_dirty(inode); */
Jiro SEKIBA9ca941d2009-11-27 19:41:06 +0900189 /* page_symlink() do this */
Ryusuke Konishid2500652009-04-06 19:01:34 -0700190
191 err = nilfs_add_nondir(dentry, inode);
192out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700193 if (!err)
194 err = nilfs_transaction_commit(dir->i_sb);
195 else
196 nilfs_transaction_abort(dir->i_sb);
197
198 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700199
200out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900201 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900202 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700203 iput(inode);
204 goto out;
205}
206
207static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
208 struct dentry *dentry)
209{
210 struct inode *inode = old_dentry->d_inode;
211 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700212 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700213
214 if (inode->i_nlink >= NILFS_LINK_MAX)
215 return -EMLINK;
216
217 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
218 if (err)
219 return err;
220
221 inode->i_ctime = CURRENT_TIME;
222 inode_inc_link_count(inode);
223 atomic_inc(&inode->i_count);
224
225 err = nilfs_add_nondir(dentry, inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700226 if (!err)
227 err = nilfs_transaction_commit(dir->i_sb);
228 else
229 nilfs_transaction_abort(dir->i_sb);
230
231 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700232}
233
234static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
235{
236 struct inode *inode;
237 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700238 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700239
240 if (dir->i_nlink >= NILFS_LINK_MAX)
241 return -EMLINK;
242
243 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
244 if (err)
245 return err;
246
Jiro SEKIBA565de402009-11-27 19:41:08 +0900247 inc_nlink(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700248
249 inode = nilfs_new_inode(dir, S_IFDIR | mode);
250 err = PTR_ERR(inode);
251 if (IS_ERR(inode))
252 goto out_dir;
253
254 inode->i_op = &nilfs_dir_inode_operations;
255 inode->i_fop = &nilfs_dir_operations;
256 inode->i_mapping->a_ops = &nilfs_aops;
257
Jiro SEKIBA565de402009-11-27 19:41:08 +0900258 inc_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700259
260 err = nilfs_make_empty(inode, dir);
261 if (err)
262 goto out_fail;
263
264 err = nilfs_add_link(dentry, inode);
265 if (err)
266 goto out_fail;
267
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900268 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700269 d_instantiate(dentry, inode);
270out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700271 if (!err)
272 err = nilfs_transaction_commit(dir->i_sb);
273 else
274 nilfs_transaction_abort(dir->i_sb);
275
276 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700277
278out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900279 drop_nlink(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900280 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900281 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700282 iput(inode);
283out_dir:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900284 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900285 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700286 goto out;
287}
288
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900289static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700290{
291 struct inode *inode;
292 struct nilfs_dir_entry *de;
293 struct page *page;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700294 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700295
Ryusuke Konishid2500652009-04-06 19:01:34 -0700296 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500297 de = nilfs_find_entry(dir, &dentry->d_name, &page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700298 if (!de)
299 goto out;
300
301 inode = dentry->d_inode;
302 err = -EIO;
303 if (le64_to_cpu(de->inode) != inode->i_ino)
304 goto out;
305
306 if (!inode->i_nlink) {
307 nilfs_warning(inode->i_sb, __func__,
308 "deleting nonexistent file (%lu), %d\n",
309 inode->i_ino, inode->i_nlink);
310 inode->i_nlink = 1;
311 }
312 err = nilfs_delete_entry(de, page);
313 if (err)
314 goto out;
315
316 inode->i_ctime = dir->i_ctime;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900317 drop_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700318 err = 0;
319out:
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900320 return err;
321}
322
323static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
324{
325 struct nilfs_transaction_info ti;
326 int err;
327
328 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
329 if (err)
330 return err;
331
332 err = nilfs_do_unlink(dir, dentry);
333
334 if (!err) {
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900335 nilfs_mark_inode_dirty(dir);
336 nilfs_mark_inode_dirty(dentry->d_inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700337 err = nilfs_transaction_commit(dir->i_sb);
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900338 } else
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700339 nilfs_transaction_abort(dir->i_sb);
340
341 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700342}
343
344static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
345{
346 struct inode *inode = dentry->d_inode;
347 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700348 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700349
350 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
351 if (err)
352 return err;
353
354 err = -ENOTEMPTY;
355 if (nilfs_empty_dir(inode)) {
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900356 err = nilfs_do_unlink(dir, dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700357 if (!err) {
358 inode->i_size = 0;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900359 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900360 nilfs_mark_inode_dirty(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900361 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900362 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700363 }
364 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700365 if (!err)
366 err = nilfs_transaction_commit(dir->i_sb);
367 else
368 nilfs_transaction_abort(dir->i_sb);
369
370 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700371}
372
373static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
374 struct inode *new_dir, struct dentry *new_dentry)
375{
376 struct inode *old_inode = old_dentry->d_inode;
377 struct inode *new_inode = new_dentry->d_inode;
378 struct page *dir_page = NULL;
379 struct nilfs_dir_entry *dir_de = NULL;
380 struct page *old_page;
381 struct nilfs_dir_entry *old_de;
382 struct nilfs_transaction_info ti;
383 int err;
384
385 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
386 if (unlikely(err))
387 return err;
388
389 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500390 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700391 if (!old_de)
392 goto out;
393
394 if (S_ISDIR(old_inode->i_mode)) {
395 err = -EIO;
396 dir_de = nilfs_dotdot(old_inode, &dir_page);
397 if (!dir_de)
398 goto out_old;
399 }
400
401 if (new_inode) {
402 struct page *new_page;
403 struct nilfs_dir_entry *new_de;
404
405 err = -ENOTEMPTY;
406 if (dir_de && !nilfs_empty_dir(new_inode))
407 goto out_dir;
408
409 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500410 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700411 if (!new_de)
412 goto out_dir;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900413 inc_nlink(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700414 nilfs_set_link(new_dir, new_de, new_page, old_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900415 nilfs_mark_inode_dirty(new_dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700416 new_inode->i_ctime = CURRENT_TIME;
417 if (dir_de)
418 drop_nlink(new_inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900419 drop_nlink(new_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900420 nilfs_mark_inode_dirty(new_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700421 } else {
422 if (dir_de) {
423 err = -EMLINK;
424 if (new_dir->i_nlink >= NILFS_LINK_MAX)
425 goto out_dir;
426 }
Jiro SEKIBA565de402009-11-27 19:41:08 +0900427 inc_nlink(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700428 err = nilfs_add_link(new_dentry, old_inode);
429 if (err) {
Jiro SEKIBA565de402009-11-27 19:41:08 +0900430 drop_nlink(old_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900431 nilfs_mark_inode_dirty(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700432 goto out_dir;
433 }
Jiro SEKIBA565de402009-11-27 19:41:08 +0900434 if (dir_de) {
435 inc_nlink(new_dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900436 nilfs_mark_inode_dirty(new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900437 }
Ryusuke Konishid2500652009-04-06 19:01:34 -0700438 }
439
440 /*
441 * Like most other Unix systems, set the ctime for inodes on a
442 * rename.
Ryusuke Konishid2500652009-04-06 19:01:34 -0700443 */
444 old_inode->i_ctime = CURRENT_TIME;
445
446 nilfs_delete_entry(old_de, old_page);
Jiro SEKIBA43f8bc22009-11-27 19:41:07 +0900447 drop_nlink(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700448
449 if (dir_de) {
450 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900451 drop_nlink(old_dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700452 }
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900453 nilfs_mark_inode_dirty(old_dir);
454 nilfs_mark_inode_dirty(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700455
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700456 err = nilfs_transaction_commit(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700457 return err;
458
459out_dir:
460 if (dir_de) {
461 kunmap(dir_page);
462 page_cache_release(dir_page);
463 }
464out_old:
465 kunmap(old_page);
466 page_cache_release(old_page);
467out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700468 nilfs_transaction_abort(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700469 return err;
470}
471
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700472const struct inode_operations nilfs_dir_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700473 .create = nilfs_create,
474 .lookup = nilfs_lookup,
475 .link = nilfs_link,
476 .unlink = nilfs_unlink,
477 .symlink = nilfs_symlink,
478 .mkdir = nilfs_mkdir,
479 .rmdir = nilfs_rmdir,
480 .mknod = nilfs_mknod,
481 .rename = nilfs_rename,
482 .setattr = nilfs_setattr,
483 .permission = nilfs_permission,
484};
485
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700486const struct inode_operations nilfs_special_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700487 .setattr = nilfs_setattr,
488 .permission = nilfs_permission,
489};
490
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700491const struct inode_operations nilfs_symlink_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700492 .readlink = generic_readlink,
493 .follow_link = page_follow_link_light,
494 .put_link = page_put_link,
495};