2 * Copyright (C) 2005, 2006
3 * Avishay Traeger (avishay@gmail.com) (avishay@il.ibm.com)
4 * Copyright (C) 2005, 2006
5 * International Business Machines
6 * Copyright (C) 2008, 2009
7 * Boaz Harrosh <bharrosh@panasas.com>
9 * Copyrights for code taken from ext2:
10 * Copyright (C) 1992, 1993, 1994, 1995
11 * Remy Card (card@masi.ibp.fr)
12 * Laboratoire MASI - Institut Blaise Pascal
13 * Universite Pierre et Marie Curie (Paris VI)
15 * linux/fs/minix/inode.c
16 * Copyright (C) 1991, 1992 Linus Torvalds
18 * This file is part of exofs.
20 * exofs is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation. Since it is based on ext2, and the only
23 * valid version of GPL for the Linux kernel is version 2, the only valid
24 * version of GPL for exofs is version 2.
26 * exofs is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with exofs; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 static inline int exofs_add_nondir(struct dentry *dentry, struct inode *inode)
40 int err = exofs_add_link(dentry, inode);
42 d_instantiate(dentry, inode);
45 inode_dec_link_count(inode);
50 static struct dentry *exofs_lookup(struct inode *dir, struct dentry *dentry,
56 if (dentry->d_name.len > EXOFS_NAME_LEN)
57 return ERR_PTR(-ENAMETOOLONG);
59 ino = exofs_inode_by_name(dir, dentry);
62 inode = exofs_iget(dir->i_sb, ino);
64 return ERR_CAST(inode);
66 return d_splice_alias(inode, dentry);
69 static int exofs_create(struct inode *dir, struct dentry *dentry, int mode,
72 struct inode *inode = exofs_new_inode(dir, mode);
73 int err = PTR_ERR(inode);
75 inode->i_op = &exofs_file_inode_operations;
76 inode->i_fop = &exofs_file_operations;
77 inode->i_mapping->a_ops = &exofs_aops;
78 mark_inode_dirty(inode);
79 err = exofs_add_nondir(dentry, inode);
84 static int exofs_mknod(struct inode *dir, struct dentry *dentry, int mode,
90 if (!new_valid_dev(rdev))
93 inode = exofs_new_inode(dir, mode);
96 init_special_inode(inode, inode->i_mode, rdev);
97 mark_inode_dirty(inode);
98 err = exofs_add_nondir(dentry, inode);
103 static int exofs_symlink(struct inode *dir, struct dentry *dentry,
106 struct super_block *sb = dir->i_sb;
107 int err = -ENAMETOOLONG;
108 unsigned l = strlen(symname)+1;
110 struct exofs_i_info *oi;
112 if (l > sb->s_blocksize)
115 inode = exofs_new_inode(dir, S_IFLNK | S_IRWXUGO);
116 err = PTR_ERR(inode);
121 if (l > sizeof(oi->i_data)) {
123 inode->i_op = &exofs_symlink_inode_operations;
124 inode->i_mapping->a_ops = &exofs_aops;
125 memset(oi->i_data, 0, sizeof(oi->i_data));
127 err = page_symlink(inode, symname, l);
132 inode->i_op = &exofs_fast_symlink_inode_operations;
133 memcpy(oi->i_data, symname, l);
136 mark_inode_dirty(inode);
138 err = exofs_add_nondir(dentry, inode);
143 inode_dec_link_count(inode);
148 static int exofs_link(struct dentry *old_dentry, struct inode *dir,
149 struct dentry *dentry)
151 struct inode *inode = old_dentry->d_inode;
153 if (inode->i_nlink >= EXOFS_LINK_MAX)
156 inode->i_ctime = CURRENT_TIME;
157 inode_inc_link_count(inode);
158 atomic_inc(&inode->i_count);
160 return exofs_add_nondir(dentry, inode);
163 static int exofs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
168 if (dir->i_nlink >= EXOFS_LINK_MAX)
171 inode_inc_link_count(dir);
173 inode = exofs_new_inode(dir, S_IFDIR | mode);
174 err = PTR_ERR(inode);
178 inode->i_op = &exofs_dir_inode_operations;
179 inode->i_fop = &exofs_dir_operations;
180 inode->i_mapping->a_ops = &exofs_aops;
182 inode_inc_link_count(inode);
184 err = exofs_make_empty(inode, dir);
188 err = exofs_add_link(dentry, inode);
192 d_instantiate(dentry, inode);
197 inode_dec_link_count(inode);
198 inode_dec_link_count(inode);
201 inode_dec_link_count(dir);
205 static int exofs_unlink(struct inode *dir, struct dentry *dentry)
207 struct inode *inode = dentry->d_inode;
208 struct exofs_dir_entry *de;
212 de = exofs_find_entry(dir, dentry, &page);
216 err = exofs_delete_entry(de, page);
220 inode->i_ctime = dir->i_ctime;
221 inode_dec_link_count(inode);
227 static int exofs_rmdir(struct inode *dir, struct dentry *dentry)
229 struct inode *inode = dentry->d_inode;
230 int err = -ENOTEMPTY;
232 if (exofs_empty_dir(inode)) {
233 err = exofs_unlink(dir, dentry);
236 inode_dec_link_count(inode);
237 inode_dec_link_count(dir);
243 static int exofs_rename(struct inode *old_dir, struct dentry *old_dentry,
244 struct inode *new_dir, struct dentry *new_dentry)
246 struct inode *old_inode = old_dentry->d_inode;
247 struct inode *new_inode = new_dentry->d_inode;
248 struct page *dir_page = NULL;
249 struct exofs_dir_entry *dir_de = NULL;
250 struct page *old_page;
251 struct exofs_dir_entry *old_de;
254 old_de = exofs_find_entry(old_dir, old_dentry, &old_page);
258 if (S_ISDIR(old_inode->i_mode)) {
260 dir_de = exofs_dotdot(old_inode, &dir_page);
266 struct page *new_page;
267 struct exofs_dir_entry *new_de;
270 if (dir_de && !exofs_empty_dir(new_inode))
274 new_de = exofs_find_entry(new_dir, new_dentry, &new_page);
277 inode_inc_link_count(old_inode);
278 err = exofs_set_link(new_dir, new_de, new_page, old_inode);
279 new_inode->i_ctime = CURRENT_TIME;
281 drop_nlink(new_inode);
282 inode_dec_link_count(new_inode);
288 if (new_dir->i_nlink >= EXOFS_LINK_MAX)
291 inode_inc_link_count(old_inode);
292 err = exofs_add_link(new_dentry, old_inode);
294 inode_dec_link_count(old_inode);
298 inode_inc_link_count(new_dir);
301 old_inode->i_ctime = CURRENT_TIME;
303 exofs_delete_entry(old_de, old_page);
304 inode_dec_link_count(old_inode);
307 err = exofs_set_link(old_inode, dir_de, dir_page, new_dir);
308 inode_dec_link_count(old_dir);
318 page_cache_release(dir_page);
322 page_cache_release(old_page);
327 const struct inode_operations exofs_dir_inode_operations = {
328 .create = exofs_create,
329 .lookup = exofs_lookup,
331 .unlink = exofs_unlink,
332 .symlink = exofs_symlink,
333 .mkdir = exofs_mkdir,
334 .rmdir = exofs_rmdir,
335 .mknod = exofs_mknod,
336 .rename = exofs_rename,
337 .setattr = exofs_setattr,
340 const struct inode_operations exofs_special_inode_operations = {
341 .setattr = exofs_setattr,