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
, struct qstr
*qstr
)
32 /* Truncate the name in place, avoids having to define a compare
34 if (qstr
->len
> SYSV_NAMELEN
) {
35 qstr
->len
= SYSV_NAMELEN
;
36 qstr
->hash
= full_name_hash(dentry
, qstr
->name
, qstr
->len
);
41 const struct dentry_operations sysv_dentry_operations
= {
45 static struct dentry
*sysv_lookup(struct inode
* dir
, struct dentry
* dentry
, unsigned int flags
)
47 struct inode
* inode
= NULL
;
50 if (dentry
->d_name
.len
> SYSV_NAMELEN
)
51 return ERR_PTR(-ENAMETOOLONG
);
52 ino
= sysv_inode_by_name(dentry
);
55 inode
= sysv_iget(dir
->i_sb
, ino
);
57 return ERR_CAST(inode
);
63 static int sysv_mknod(struct inode
* dir
, struct dentry
* dentry
, umode_t mode
, dev_t rdev
)
68 if (!old_valid_dev(rdev
))
71 inode
= sysv_new_inode(dir
, mode
);
75 sysv_set_inode(inode
, rdev
);
76 mark_inode_dirty(inode
);
77 err
= add_nondir(dentry
, inode
);
82 static int sysv_create(struct inode
* dir
, struct dentry
* dentry
, umode_t mode
, bool excl
)
84 return sysv_mknod(dir
, dentry
, mode
, 0);
87 static int sysv_symlink(struct inode
* dir
, struct dentry
* dentry
,
90 int err
= -ENAMETOOLONG
;
91 int l
= strlen(symname
)+1;
94 if (l
> dir
->i_sb
->s_blocksize
)
97 inode
= sysv_new_inode(dir
, S_IFLNK
|0777);
102 sysv_set_inode(inode
, 0);
103 err
= page_symlink(inode
, symname
, l
);
107 mark_inode_dirty(inode
);
108 err
= add_nondir(dentry
, inode
);
113 inode_dec_link_count(inode
);
118 static int sysv_link(struct dentry
* old_dentry
, struct inode
* dir
,
119 struct dentry
* dentry
)
121 struct inode
*inode
= d_inode(old_dentry
);
123 inode
->i_ctime
= current_time(inode
);
124 inode_inc_link_count(inode
);
127 return add_nondir(dentry
, inode
);
130 static int sysv_mkdir(struct inode
* dir
, struct dentry
*dentry
, umode_t mode
)
132 struct inode
* inode
;
135 inode_inc_link_count(dir
);
137 inode
= sysv_new_inode(dir
, S_IFDIR
|mode
);
138 err
= PTR_ERR(inode
);
142 sysv_set_inode(inode
, 0);
144 inode_inc_link_count(inode
);
146 err
= sysv_make_empty(inode
, dir
);
150 err
= sysv_add_link(dentry
, inode
);
154 d_instantiate(dentry
, inode
);
159 inode_dec_link_count(inode
);
160 inode_dec_link_count(inode
);
163 inode_dec_link_count(dir
);
167 static int sysv_unlink(struct inode
* dir
, struct dentry
* dentry
)
169 struct inode
* inode
= d_inode(dentry
);
171 struct sysv_dir_entry
* de
;
174 de
= sysv_find_entry(dentry
, &page
);
178 err
= sysv_delete_entry (de
, page
);
182 inode
->i_ctime
= dir
->i_ctime
;
183 inode_dec_link_count(inode
);
188 static int sysv_rmdir(struct inode
* dir
, struct dentry
* dentry
)
190 struct inode
*inode
= d_inode(dentry
);
191 int err
= -ENOTEMPTY
;
193 if (sysv_empty_dir(inode
)) {
194 err
= sysv_unlink(dir
, dentry
);
197 inode_dec_link_count(inode
);
198 inode_dec_link_count(dir
);
205 * Anybody can rename anything with this: the permission checks are left to the
206 * higher-level routines.
208 static int sysv_rename(struct inode
* old_dir
, struct dentry
* old_dentry
,
209 struct inode
* new_dir
, struct dentry
* new_dentry
,
212 struct inode
* old_inode
= d_inode(old_dentry
);
213 struct inode
* new_inode
= d_inode(new_dentry
);
214 struct page
* dir_page
= NULL
;
215 struct sysv_dir_entry
* dir_de
= NULL
;
216 struct page
* old_page
;
217 struct sysv_dir_entry
* old_de
;
220 if (flags
& ~RENAME_NOREPLACE
)
223 old_de
= sysv_find_entry(old_dentry
, &old_page
);
227 if (S_ISDIR(old_inode
->i_mode
)) {
229 dir_de
= sysv_dotdot(old_inode
, &dir_page
);
235 struct page
* new_page
;
236 struct sysv_dir_entry
* new_de
;
239 if (dir_de
&& !sysv_empty_dir(new_inode
))
243 new_de
= sysv_find_entry(new_dentry
, &new_page
);
246 sysv_set_link(new_de
, new_page
, old_inode
);
247 new_inode
->i_ctime
= current_time(new_inode
);
249 drop_nlink(new_inode
);
250 inode_dec_link_count(new_inode
);
252 err
= sysv_add_link(new_dentry
, old_inode
);
256 inode_inc_link_count(new_dir
);
259 sysv_delete_entry(old_de
, old_page
);
260 mark_inode_dirty(old_inode
);
263 sysv_set_link(dir_de
, dir_page
, new_dir
);
264 inode_dec_link_count(old_dir
);
281 * directories can handle most operations...
283 const struct inode_operations sysv_dir_inode_operations
= {
284 .create
= sysv_create
,
285 .lookup
= sysv_lookup
,
287 .unlink
= sysv_unlink
,
288 .symlink
= sysv_symlink
,
292 .rename
= sysv_rename
,
293 .getattr
= sysv_getattr
,