2 * linux/fs/sysv/namei.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Copyright (C) 1993 Pascal Haible, Bruno Haible
11 * Copyright (C) 1993 Bruno Haible
12 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
15 #include <linux/pagemap.h>
18 static int add_nondir(struct dentry
*dentry
, struct inode
*inode
)
20 int err
= sysv_add_link(dentry
, inode
);
22 d_instantiate(dentry
, inode
);
25 inode_dec_link_count(inode
);
30 static int sysv_hash(const struct dentry
*dentry
, const struct inode
*inode
,
33 /* Truncate the name in place, avoids having to define a compare
35 if (qstr
->len
> SYSV_NAMELEN
) {
36 qstr
->len
= SYSV_NAMELEN
;
37 qstr
->hash
= full_name_hash(qstr
->name
, qstr
->len
);
42 const struct dentry_operations sysv_dentry_operations
= {
46 static struct dentry
*sysv_lookup(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
48 struct inode
* inode
= NULL
;
51 if (dentry
->d_name
.len
> SYSV_NAMELEN
)
52 return ERR_PTR(-ENAMETOOLONG
);
53 ino
= sysv_inode_by_name(dentry
);
56 inode
= sysv_iget(dir
->i_sb
, ino
);
58 return ERR_CAST(inode
);
64 static int sysv_mknod(struct inode
* dir
, struct dentry
* dentry
, int mode
, dev_t rdev
)
69 if (!old_valid_dev(rdev
))
72 inode
= sysv_new_inode(dir
, mode
);
76 sysv_set_inode(inode
, rdev
);
77 mark_inode_dirty(inode
);
78 err
= add_nondir(dentry
, inode
);
83 static int sysv_create(struct inode
* dir
, struct dentry
* dentry
, int mode
, struct nameidata
*nd
)
85 return sysv_mknod(dir
, dentry
, mode
, 0);
88 static int sysv_symlink(struct inode
* dir
, struct dentry
* dentry
,
91 int err
= -ENAMETOOLONG
;
92 int l
= strlen(symname
)+1;
95 if (l
> dir
->i_sb
->s_blocksize
)
98 inode
= sysv_new_inode(dir
, S_IFLNK
|0777);
103 sysv_set_inode(inode
, 0);
104 err
= page_symlink(inode
, symname
, l
);
108 mark_inode_dirty(inode
);
109 err
= add_nondir(dentry
, inode
);
114 inode_dec_link_count(inode
);
119 static int sysv_link(struct dentry
* old_dentry
, struct inode
* dir
,
120 struct dentry
* dentry
)
122 struct inode
*inode
= old_dentry
->d_inode
;
124 if (inode
->i_nlink
>= SYSV_SB(inode
->i_sb
)->s_link_max
)
127 inode
->i_ctime
= CURRENT_TIME_SEC
;
128 inode_inc_link_count(inode
);
131 return add_nondir(dentry
, inode
);
134 static int sysv_mkdir(struct inode
* dir
, struct dentry
*dentry
, int mode
)
136 struct inode
* inode
;
139 if (dir
->i_nlink
>= SYSV_SB(dir
->i_sb
)->s_link_max
)
141 inode_inc_link_count(dir
);
143 inode
= sysv_new_inode(dir
, S_IFDIR
|mode
);
144 err
= PTR_ERR(inode
);
148 sysv_set_inode(inode
, 0);
150 inode_inc_link_count(inode
);
152 err
= sysv_make_empty(inode
, dir
);
156 err
= sysv_add_link(dentry
, inode
);
160 d_instantiate(dentry
, inode
);
165 inode_dec_link_count(inode
);
166 inode_dec_link_count(inode
);
169 inode_dec_link_count(dir
);
173 static int sysv_unlink(struct inode
* dir
, struct dentry
* dentry
)
175 struct inode
* inode
= dentry
->d_inode
;
177 struct sysv_dir_entry
* de
;
180 de
= sysv_find_entry(dentry
, &page
);
184 err
= sysv_delete_entry (de
, page
);
188 inode
->i_ctime
= dir
->i_ctime
;
189 inode_dec_link_count(inode
);
194 static int sysv_rmdir(struct inode
* dir
, struct dentry
* dentry
)
196 struct inode
*inode
= dentry
->d_inode
;
197 int err
= -ENOTEMPTY
;
199 if (sysv_empty_dir(inode
)) {
200 err
= sysv_unlink(dir
, dentry
);
203 inode_dec_link_count(inode
);
204 inode_dec_link_count(dir
);
211 * Anybody can rename anything with this: the permission checks are left to the
212 * higher-level routines.
214 static int sysv_rename(struct inode
* old_dir
, struct dentry
* old_dentry
,
215 struct inode
* new_dir
, struct dentry
* new_dentry
)
217 struct inode
* old_inode
= old_dentry
->d_inode
;
218 struct inode
* new_inode
= new_dentry
->d_inode
;
219 struct page
* dir_page
= NULL
;
220 struct sysv_dir_entry
* dir_de
= NULL
;
221 struct page
* old_page
;
222 struct sysv_dir_entry
* old_de
;
225 old_de
= sysv_find_entry(old_dentry
, &old_page
);
229 if (S_ISDIR(old_inode
->i_mode
)) {
231 dir_de
= sysv_dotdot(old_inode
, &dir_page
);
237 struct page
* new_page
;
238 struct sysv_dir_entry
* new_de
;
241 if (dir_de
&& !sysv_empty_dir(new_inode
))
245 new_de
= sysv_find_entry(new_dentry
, &new_page
);
248 sysv_set_link(new_de
, new_page
, old_inode
);
249 new_inode
->i_ctime
= CURRENT_TIME_SEC
;
251 drop_nlink(new_inode
);
252 inode_dec_link_count(new_inode
);
256 if (new_dir
->i_nlink
>= SYSV_SB(new_dir
->i_sb
)->s_link_max
)
259 err
= sysv_add_link(new_dentry
, old_inode
);
263 inode_inc_link_count(new_dir
);
266 sysv_delete_entry(old_de
, old_page
);
267 mark_inode_dirty(old_inode
);
270 sysv_set_link(dir_de
, dir_page
, new_dir
);
271 inode_dec_link_count(old_dir
);
278 page_cache_release(dir_page
);
282 page_cache_release(old_page
);
288 * directories can handle most operations...
290 const struct inode_operations sysv_dir_inode_operations
= {
291 .create
= sysv_create
,
292 .lookup
= sysv_lookup
,
294 .unlink
= sysv_unlink
,
295 .symlink
= sysv_symlink
,
299 .rename
= sysv_rename
,
300 .getattr
= sysv_getattr
,