blob: ad6ed2cf19b4b5d31f3b7a80cfe11377b5acfceb [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) {
73 inode = nilfs_iget(dir->i_sb, ino);
74 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
90 inode = nilfs_iget(child->d_inode->i_sb, ino);
91 if (IS_ERR(inode))
92 return ERR_CAST(inode);
93 return d_obtain_alias(inode);
94}
95
96/*
97 * By the time this is called, we already have created
98 * the directory cache entry for the new file, but it
99 * is so far negative - it has no inode.
100 *
101 * If the create succeeds, we fill in the inode information
102 * with d_instantiate().
103 */
104static int nilfs_create(struct inode *dir, struct dentry *dentry, int mode,
105 struct nameidata *nd)
106{
107 struct inode *inode;
108 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700109 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700110
111 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
112 if (err)
113 return err;
114 inode = nilfs_new_inode(dir, mode);
115 err = PTR_ERR(inode);
116 if (!IS_ERR(inode)) {
117 inode->i_op = &nilfs_file_inode_operations;
118 inode->i_fop = &nilfs_file_operations;
119 inode->i_mapping->a_ops = &nilfs_aops;
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900120 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700121 err = nilfs_add_nondir(dentry, inode);
122 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700123 if (!err)
124 err = nilfs_transaction_commit(dir->i_sb);
125 else
126 nilfs_transaction_abort(dir->i_sb);
127
128 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700129}
130
131static int
132nilfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
133{
134 struct inode *inode;
135 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700136 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700137
138 if (!new_valid_dev(rdev))
139 return -EINVAL;
140
141 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
142 if (err)
143 return err;
144 inode = nilfs_new_inode(dir, mode);
145 err = PTR_ERR(inode);
146 if (!IS_ERR(inode)) {
147 init_special_inode(inode, inode->i_mode, rdev);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900148 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700149 err = nilfs_add_nondir(dentry, inode);
150 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700151 if (!err)
152 err = nilfs_transaction_commit(dir->i_sb);
153 else
154 nilfs_transaction_abort(dir->i_sb);
155
156 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700157}
158
159static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
160 const char *symname)
161{
162 struct nilfs_transaction_info ti;
163 struct super_block *sb = dir->i_sb;
164 unsigned l = strlen(symname)+1;
165 struct inode *inode;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700166 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700167
168 if (l > sb->s_blocksize)
169 return -ENAMETOOLONG;
170
171 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
172 if (err)
173 return err;
174
175 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
176 err = PTR_ERR(inode);
177 if (IS_ERR(inode))
178 goto out;
179
180 /* slow symlink */
181 inode->i_op = &nilfs_symlink_inode_operations;
182 inode->i_mapping->a_ops = &nilfs_aops;
183 err = page_symlink(inode, symname, l);
184 if (err)
185 goto out_fail;
186
187 /* mark_inode_dirty(inode); */
Jiro SEKIBA9ca941d2009-11-27 19:41:06 +0900188 /* page_symlink() do this */
Ryusuke Konishid2500652009-04-06 19:01:34 -0700189
190 err = nilfs_add_nondir(dentry, inode);
191out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700192 if (!err)
193 err = nilfs_transaction_commit(dir->i_sb);
194 else
195 nilfs_transaction_abort(dir->i_sb);
196
197 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700198
199out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900200 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900201 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700202 iput(inode);
203 goto out;
204}
205
206static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
207 struct dentry *dentry)
208{
209 struct inode *inode = old_dentry->d_inode;
210 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700211 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700212
213 if (inode->i_nlink >= NILFS_LINK_MAX)
214 return -EMLINK;
215
216 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
217 if (err)
218 return err;
219
220 inode->i_ctime = CURRENT_TIME;
221 inode_inc_link_count(inode);
222 atomic_inc(&inode->i_count);
223
224 err = nilfs_add_nondir(dentry, inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700225 if (!err)
226 err = nilfs_transaction_commit(dir->i_sb);
227 else
228 nilfs_transaction_abort(dir->i_sb);
229
230 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700231}
232
233static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
234{
235 struct inode *inode;
236 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700237 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700238
239 if (dir->i_nlink >= NILFS_LINK_MAX)
240 return -EMLINK;
241
242 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
243 if (err)
244 return err;
245
Jiro SEKIBA565de402009-11-27 19:41:08 +0900246 inc_nlink(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700247
248 inode = nilfs_new_inode(dir, S_IFDIR | mode);
249 err = PTR_ERR(inode);
250 if (IS_ERR(inode))
251 goto out_dir;
252
253 inode->i_op = &nilfs_dir_inode_operations;
254 inode->i_fop = &nilfs_dir_operations;
255 inode->i_mapping->a_ops = &nilfs_aops;
256
Jiro SEKIBA565de402009-11-27 19:41:08 +0900257 inc_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700258
259 err = nilfs_make_empty(inode, dir);
260 if (err)
261 goto out_fail;
262
263 err = nilfs_add_link(dentry, inode);
264 if (err)
265 goto out_fail;
266
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900267 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700268 d_instantiate(dentry, inode);
269out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700270 if (!err)
271 err = nilfs_transaction_commit(dir->i_sb);
272 else
273 nilfs_transaction_abort(dir->i_sb);
274
275 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700276
277out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900278 drop_nlink(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900279 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900280 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700281 iput(inode);
282out_dir:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900283 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900284 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700285 goto out;
286}
287
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900288static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700289{
290 struct inode *inode;
291 struct nilfs_dir_entry *de;
292 struct page *page;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700293 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700294
Ryusuke Konishid2500652009-04-06 19:01:34 -0700295 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500296 de = nilfs_find_entry(dir, &dentry->d_name, &page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700297 if (!de)
298 goto out;
299
300 inode = dentry->d_inode;
301 err = -EIO;
302 if (le64_to_cpu(de->inode) != inode->i_ino)
303 goto out;
304
305 if (!inode->i_nlink) {
306 nilfs_warning(inode->i_sb, __func__,
307 "deleting nonexistent file (%lu), %d\n",
308 inode->i_ino, inode->i_nlink);
309 inode->i_nlink = 1;
310 }
311 err = nilfs_delete_entry(de, page);
312 if (err)
313 goto out;
314
315 inode->i_ctime = dir->i_ctime;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900316 drop_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700317 err = 0;
318out:
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900319 return err;
320}
321
322static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
323{
324 struct nilfs_transaction_info ti;
325 int err;
326
327 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
328 if (err)
329 return err;
330
331 err = nilfs_do_unlink(dir, dentry);
332
333 if (!err) {
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900334 nilfs_mark_inode_dirty(dir);
335 nilfs_mark_inode_dirty(dentry->d_inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700336 err = nilfs_transaction_commit(dir->i_sb);
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900337 } else
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700338 nilfs_transaction_abort(dir->i_sb);
339
340 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700341}
342
343static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
344{
345 struct inode *inode = dentry->d_inode;
346 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700347 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700348
349 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
350 if (err)
351 return err;
352
353 err = -ENOTEMPTY;
354 if (nilfs_empty_dir(inode)) {
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900355 err = nilfs_do_unlink(dir, dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700356 if (!err) {
357 inode->i_size = 0;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900358 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900359 nilfs_mark_inode_dirty(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900360 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900361 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700362 }
363 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700364 if (!err)
365 err = nilfs_transaction_commit(dir->i_sb);
366 else
367 nilfs_transaction_abort(dir->i_sb);
368
369 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700370}
371
372static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
373 struct inode *new_dir, struct dentry *new_dentry)
374{
375 struct inode *old_inode = old_dentry->d_inode;
376 struct inode *new_inode = new_dentry->d_inode;
377 struct page *dir_page = NULL;
378 struct nilfs_dir_entry *dir_de = NULL;
379 struct page *old_page;
380 struct nilfs_dir_entry *old_de;
381 struct nilfs_transaction_info ti;
382 int err;
383
384 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
385 if (unlikely(err))
386 return err;
387
388 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500389 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700390 if (!old_de)
391 goto out;
392
393 if (S_ISDIR(old_inode->i_mode)) {
394 err = -EIO;
395 dir_de = nilfs_dotdot(old_inode, &dir_page);
396 if (!dir_de)
397 goto out_old;
398 }
399
400 if (new_inode) {
401 struct page *new_page;
402 struct nilfs_dir_entry *new_de;
403
404 err = -ENOTEMPTY;
405 if (dir_de && !nilfs_empty_dir(new_inode))
406 goto out_dir;
407
408 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500409 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700410 if (!new_de)
411 goto out_dir;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900412 inc_nlink(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700413 nilfs_set_link(new_dir, new_de, new_page, old_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900414 nilfs_mark_inode_dirty(new_dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700415 new_inode->i_ctime = CURRENT_TIME;
416 if (dir_de)
417 drop_nlink(new_inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900418 drop_nlink(new_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900419 nilfs_mark_inode_dirty(new_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700420 } else {
421 if (dir_de) {
422 err = -EMLINK;
423 if (new_dir->i_nlink >= NILFS_LINK_MAX)
424 goto out_dir;
425 }
Jiro SEKIBA565de402009-11-27 19:41:08 +0900426 inc_nlink(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700427 err = nilfs_add_link(new_dentry, old_inode);
428 if (err) {
Jiro SEKIBA565de402009-11-27 19:41:08 +0900429 drop_nlink(old_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900430 nilfs_mark_inode_dirty(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700431 goto out_dir;
432 }
Jiro SEKIBA565de402009-11-27 19:41:08 +0900433 if (dir_de) {
434 inc_nlink(new_dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900435 nilfs_mark_inode_dirty(new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900436 }
Ryusuke Konishid2500652009-04-06 19:01:34 -0700437 }
438
439 /*
440 * Like most other Unix systems, set the ctime for inodes on a
441 * rename.
Ryusuke Konishid2500652009-04-06 19:01:34 -0700442 */
443 old_inode->i_ctime = CURRENT_TIME;
444
445 nilfs_delete_entry(old_de, old_page);
Jiro SEKIBA43f8bc22009-11-27 19:41:07 +0900446 drop_nlink(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700447
448 if (dir_de) {
449 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900450 drop_nlink(old_dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700451 }
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900452 nilfs_mark_inode_dirty(old_dir);
453 nilfs_mark_inode_dirty(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700454
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700455 err = nilfs_transaction_commit(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700456 return err;
457
458out_dir:
459 if (dir_de) {
460 kunmap(dir_page);
461 page_cache_release(dir_page);
462 }
463out_old:
464 kunmap(old_page);
465 page_cache_release(old_page);
466out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700467 nilfs_transaction_abort(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700468 return err;
469}
470
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700471const struct inode_operations nilfs_dir_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700472 .create = nilfs_create,
473 .lookup = nilfs_lookup,
474 .link = nilfs_link,
475 .unlink = nilfs_unlink,
476 .symlink = nilfs_symlink,
477 .mkdir = nilfs_mkdir,
478 .rmdir = nilfs_rmdir,
479 .mknod = nilfs_mknod,
480 .rename = nilfs_rename,
481 .setattr = nilfs_setattr,
482 .permission = nilfs_permission,
483};
484
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700485const struct inode_operations nilfs_special_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700486 .setattr = nilfs_setattr,
487 .permission = nilfs_permission,
488};
489
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700490const struct inode_operations nilfs_symlink_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700491 .readlink = generic_readlink,
492 .follow_link = page_follow_link_light,
493 .put_link = page_put_link,
494};