2 * namei.c - NILFS pathname lookup operations.
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Modified for NILFS by Amagai Yoshiji <amagai@osrg.net>,
21 * Ryusuke Konishi <ryusuke@osrg.net>
24 * linux/fs/ext2/namei.c
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)
33 * linux/fs/minix/namei.c
35 * Copyright (C) 1991, 1992 Linus Torvalds
37 * Big-endian to little-endian byte-swapping/bitmaps by
38 * David S. Miller (davem@caip.rutgers.edu), 1995
41 #include <linux/pagemap.h>
45 static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
47 int err = nilfs_add_link(dentry, inode);
49 d_instantiate(dentry, inode);
52 inode_dec_link_count(inode);
61 static struct dentry *
62 nilfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
67 if (dentry->d_name.len > NILFS_NAME_LEN)
68 return ERR_PTR(-ENAMETOOLONG);
70 ino = nilfs_inode_by_name(dir, dentry);
73 inode = nilfs_iget(dir->i_sb, ino);
75 return ERR_CAST(inode);
77 return d_splice_alias(inode, dentry);
80 struct dentry *nilfs_get_parent(struct dentry *child)
86 dotdot.d_name.name = "..";
87 dotdot.d_name.len = 2;
89 ino = nilfs_inode_by_name(child->d_inode, &dotdot);
91 return ERR_PTR(-ENOENT);
93 inode = nilfs_iget(child->d_inode->i_sb, ino);
95 return ERR_CAST(inode);
96 return d_obtain_alias(inode);
100 * By the time this is called, we already have created
101 * the directory cache entry for the new file, but it
102 * is so far negative - it has no inode.
104 * If the create succeeds, we fill in the inode information
105 * with d_instantiate().
107 static int nilfs_create(struct inode *dir, struct dentry *dentry, int mode,
108 struct nameidata *nd)
111 struct nilfs_transaction_info ti;
114 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
117 inode = nilfs_new_inode(dir, mode);
118 err = PTR_ERR(inode);
119 if (!IS_ERR(inode)) {
120 inode->i_op = &nilfs_file_inode_operations;
121 inode->i_fop = &nilfs_file_operations;
122 inode->i_mapping->a_ops = &nilfs_aops;
123 mark_inode_dirty(inode);
124 err = nilfs_add_nondir(dentry, inode);
127 err = nilfs_transaction_commit(dir->i_sb);
129 nilfs_transaction_abort(dir->i_sb);
135 nilfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
138 struct nilfs_transaction_info ti;
141 if (!new_valid_dev(rdev))
144 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
147 inode = nilfs_new_inode(dir, mode);
148 err = PTR_ERR(inode);
149 if (!IS_ERR(inode)) {
150 init_special_inode(inode, inode->i_mode, rdev);
151 mark_inode_dirty(inode);
152 err = nilfs_add_nondir(dentry, inode);
155 err = nilfs_transaction_commit(dir->i_sb);
157 nilfs_transaction_abort(dir->i_sb);
162 static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
165 struct nilfs_transaction_info ti;
166 struct super_block *sb = dir->i_sb;
167 unsigned l = strlen(symname)+1;
171 if (l > sb->s_blocksize)
172 return -ENAMETOOLONG;
174 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
178 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
179 err = PTR_ERR(inode);
184 inode->i_op = &nilfs_symlink_inode_operations;
185 inode->i_mapping->a_ops = &nilfs_aops;
186 err = page_symlink(inode, symname, l);
190 /* mark_inode_dirty(inode); */
191 /* page_symlink() do this */
193 err = nilfs_add_nondir(dentry, inode);
196 err = nilfs_transaction_commit(dir->i_sb);
198 nilfs_transaction_abort(dir->i_sb);
204 mark_inode_dirty(inode);
209 static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
210 struct dentry *dentry)
212 struct inode *inode = old_dentry->d_inode;
213 struct nilfs_transaction_info ti;
216 if (inode->i_nlink >= NILFS_LINK_MAX)
219 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
223 inode->i_ctime = CURRENT_TIME;
224 inode_inc_link_count(inode);
225 atomic_inc(&inode->i_count);
227 err = nilfs_add_nondir(dentry, inode);
229 err = nilfs_transaction_commit(dir->i_sb);
231 nilfs_transaction_abort(dir->i_sb);
236 static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
239 struct nilfs_transaction_info ti;
242 if (dir->i_nlink >= NILFS_LINK_MAX)
245 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
250 mark_inode_dirty(dir);
252 inode = nilfs_new_inode(dir, S_IFDIR | mode);
253 err = PTR_ERR(inode);
257 inode->i_op = &nilfs_dir_inode_operations;
258 inode->i_fop = &nilfs_dir_operations;
259 inode->i_mapping->a_ops = &nilfs_aops;
262 mark_inode_dirty(inode);
264 err = nilfs_make_empty(inode, dir);
268 err = nilfs_add_link(dentry, inode);
272 d_instantiate(dentry, inode);
275 err = nilfs_transaction_commit(dir->i_sb);
277 nilfs_transaction_abort(dir->i_sb);
283 mark_inode_dirty(inode);
285 mark_inode_dirty(inode);
289 mark_inode_dirty(dir);
293 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
296 struct nilfs_dir_entry *de;
298 struct nilfs_transaction_info ti;
301 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
306 de = nilfs_find_entry(dir, dentry, &page);
310 inode = dentry->d_inode;
312 if (le64_to_cpu(de->inode) != inode->i_ino)
315 if (!inode->i_nlink) {
316 nilfs_warning(inode->i_sb, __func__,
317 "deleting nonexistent file (%lu), %d\n",
318 inode->i_ino, inode->i_nlink);
321 err = nilfs_delete_entry(de, page);
325 inode->i_ctime = dir->i_ctime;
327 mark_inode_dirty(inode);
331 err = nilfs_transaction_commit(dir->i_sb);
333 nilfs_transaction_abort(dir->i_sb);
338 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
340 struct inode *inode = dentry->d_inode;
341 struct nilfs_transaction_info ti;
344 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
349 if (nilfs_empty_dir(inode)) {
350 err = nilfs_unlink(dir, dentry);
354 mark_inode_dirty(inode);
356 mark_inode_dirty(dir);
360 err = nilfs_transaction_commit(dir->i_sb);
362 nilfs_transaction_abort(dir->i_sb);
367 static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
368 struct inode *new_dir, struct dentry *new_dentry)
370 struct inode *old_inode = old_dentry->d_inode;
371 struct inode *new_inode = new_dentry->d_inode;
372 struct page *dir_page = NULL;
373 struct nilfs_dir_entry *dir_de = NULL;
374 struct page *old_page;
375 struct nilfs_dir_entry *old_de;
376 struct nilfs_transaction_info ti;
379 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
384 old_de = nilfs_find_entry(old_dir, old_dentry, &old_page);
388 if (S_ISDIR(old_inode->i_mode)) {
390 dir_de = nilfs_dotdot(old_inode, &dir_page);
396 struct page *new_page;
397 struct nilfs_dir_entry *new_de;
400 if (dir_de && !nilfs_empty_dir(new_inode))
404 new_de = nilfs_find_entry(new_dir, new_dentry, &new_page);
407 inc_nlink(old_inode);
408 mark_inode_dirty(old_inode);
409 nilfs_set_link(new_dir, new_de, new_page, old_inode);
410 mark_inode_dirty(new_dir);
411 new_inode->i_ctime = CURRENT_TIME;
413 drop_nlink(new_inode);
414 drop_nlink(new_inode);
415 mark_inode_dirty(new_inode);
419 if (new_dir->i_nlink >= NILFS_LINK_MAX)
422 inc_nlink(old_inode);
423 mark_inode_dirty(old_inode);
424 err = nilfs_add_link(new_dentry, old_inode);
426 drop_nlink(old_inode);
427 mark_inode_dirty(old_inode);
432 mark_inode_dirty(new_dir);
437 * Like most other Unix systems, set the ctime for inodes on a
440 old_inode->i_ctime = CURRENT_TIME;
442 nilfs_delete_entry(old_de, old_page);
443 drop_nlink(old_inode);
446 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
448 mark_inode_dirty(old_dir);
450 mark_inode_dirty(old_inode);
452 err = nilfs_transaction_commit(old_dir->i_sb);
458 page_cache_release(dir_page);
462 page_cache_release(old_page);
464 nilfs_transaction_abort(old_dir->i_sb);
468 const struct inode_operations nilfs_dir_inode_operations = {
469 .create = nilfs_create,
470 .lookup = nilfs_lookup,
472 .unlink = nilfs_unlink,
473 .symlink = nilfs_symlink,
474 .mkdir = nilfs_mkdir,
475 .rmdir = nilfs_rmdir,
476 .mknod = nilfs_mknod,
477 .rename = nilfs_rename,
478 .setattr = nilfs_setattr,
479 .permission = nilfs_permission,
482 const struct inode_operations nilfs_special_inode_operations = {
483 .setattr = nilfs_setattr,
484 .permission = nilfs_permission,
487 const struct inode_operations nilfs_symlink_inode_operations = {
488 .readlink = generic_readlink,
489 .follow_link = page_follow_link_light,
490 .put_link = page_put_link,