blob: e474fbcf8bde991924da62e321d2be7eae8ac5f6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/sysv/namei.c
3 *
4 * minix/namei.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * coh/namei.c
8 * Copyright (C) 1993 Pascal Haible, Bruno Haible
9 *
10 * sysv/namei.c
11 * Copyright (C) 1993 Bruno Haible
12 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
13 */
14
15#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "sysv.h"
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018static int add_nondir(struct dentry *dentry, struct inode *inode)
19{
20 int err = sysv_add_link(dentry, inode);
21 if (!err) {
22 d_instantiate(dentry, inode);
23 return 0;
24 }
Alexey Dobriyan4e907c32006-03-23 03:00:52 -080025 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 iput(inode);
27 return err;
28}
29
Nick Pigginb1e6a012011-01-07 17:49:28 +110030static int sysv_hash(const struct dentry *dentry, const struct inode *inode,
31 struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 /* Truncate the name in place, avoids having to define a compare
34 function. */
35 if (qstr->len > SYSV_NAMELEN) {
36 qstr->len = SYSV_NAMELEN;
37 qstr->hash = full_name_hash(qstr->name, qstr->len);
38 }
39 return 0;
40}
41
Al Viroe16404e2009-02-20 05:55:13 +000042const struct dentry_operations sysv_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .d_hash = sysv_hash,
44};
45
46static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
47{
48 struct inode * inode = NULL;
49 ino_t ino;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (dentry->d_name.len > SYSV_NAMELEN)
52 return ERR_PTR(-ENAMETOOLONG);
53 ino = sysv_inode_by_name(dentry);
54
55 if (ino) {
David Howellsb8e13432008-02-07 00:15:47 -080056 inode = sysv_iget(dir->i_sb, ino);
57 if (IS_ERR(inode))
58 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60 d_add(dentry, inode);
61 return NULL;
62}
63
64static int sysv_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev)
65{
66 struct inode * inode;
67 int err;
68
69 if (!old_valid_dev(rdev))
70 return -EINVAL;
71
72 inode = sysv_new_inode(dir, mode);
73 err = PTR_ERR(inode);
74
75 if (!IS_ERR(inode)) {
76 sysv_set_inode(inode, rdev);
77 mark_inode_dirty(inode);
78 err = add_nondir(dentry, inode);
79 }
80 return err;
81}
82
83static int sysv_create(struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
84{
85 return sysv_mknod(dir, dentry, mode, 0);
86}
87
88static int sysv_symlink(struct inode * dir, struct dentry * dentry,
89 const char * symname)
90{
91 int err = -ENAMETOOLONG;
92 int l = strlen(symname)+1;
93 struct inode * inode;
94
95 if (l > dir->i_sb->s_blocksize)
96 goto out;
97
98 inode = sysv_new_inode(dir, S_IFLNK|0777);
99 err = PTR_ERR(inode);
100 if (IS_ERR(inode))
101 goto out;
102
103 sysv_set_inode(inode, 0);
104 err = page_symlink(inode, symname, l);
105 if (err)
106 goto out_fail;
107
108 mark_inode_dirty(inode);
109 err = add_nondir(dentry, inode);
110out:
111 return err;
112
113out_fail:
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800114 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 iput(inode);
116 goto out;
117}
118
119static int sysv_link(struct dentry * old_dentry, struct inode * dir,
120 struct dentry * dentry)
121{
122 struct inode *inode = old_dentry->d_inode;
123
124 if (inode->i_nlink >= SYSV_SB(inode->i_sb)->s_link_max)
125 return -EMLINK;
126
127 inode->i_ctime = CURRENT_TIME_SEC;
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800128 inode_inc_link_count(inode);
Al Viro7de9c6e2010-10-23 11:11:40 -0400129 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return add_nondir(dentry, inode);
132}
133
134static int sysv_mkdir(struct inode * dir, struct dentry *dentry, int mode)
135{
136 struct inode * inode;
137 int err = -EMLINK;
138
139 if (dir->i_nlink >= SYSV_SB(dir->i_sb)->s_link_max)
140 goto out;
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800141 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 inode = sysv_new_inode(dir, S_IFDIR|mode);
144 err = PTR_ERR(inode);
145 if (IS_ERR(inode))
146 goto out_dir;
147
148 sysv_set_inode(inode, 0);
149
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800150 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 err = sysv_make_empty(inode, dir);
153 if (err)
154 goto out_fail;
155
156 err = sysv_add_link(dentry, inode);
157 if (err)
158 goto out_fail;
159
160 d_instantiate(dentry, inode);
161out:
162 return err;
163
164out_fail:
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800165 inode_dec_link_count(inode);
166 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 iput(inode);
168out_dir:
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800169 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto out;
171}
172
173static int sysv_unlink(struct inode * dir, struct dentry * dentry)
174{
175 struct inode * inode = dentry->d_inode;
176 struct page * page;
177 struct sysv_dir_entry * de;
178 int err = -ENOENT;
179
180 de = sysv_find_entry(dentry, &page);
181 if (!de)
182 goto out;
183
184 err = sysv_delete_entry (de, page);
185 if (err)
186 goto out;
187
188 inode->i_ctime = dir->i_ctime;
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800189 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190out:
191 return err;
192}
193
194static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
195{
196 struct inode *inode = dentry->d_inode;
197 int err = -ENOTEMPTY;
198
199 if (sysv_empty_dir(inode)) {
200 err = sysv_unlink(dir, dentry);
201 if (!err) {
202 inode->i_size = 0;
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800203 inode_dec_link_count(inode);
204 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 }
207 return err;
208}
209
210/*
211 * Anybody can rename anything with this: the permission checks are left to the
212 * higher-level routines.
213 */
214static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry,
215 struct inode * new_dir, struct dentry * new_dentry)
216{
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;
223 int err = -ENOENT;
224
225 old_de = sysv_find_entry(old_dentry, &old_page);
226 if (!old_de)
227 goto out;
228
229 if (S_ISDIR(old_inode->i_mode)) {
230 err = -EIO;
231 dir_de = sysv_dotdot(old_inode, &dir_page);
232 if (!dir_de)
233 goto out_old;
234 }
235
236 if (new_inode) {
237 struct page * new_page;
238 struct sysv_dir_entry * new_de;
239
240 err = -ENOTEMPTY;
241 if (dir_de && !sysv_empty_dir(new_inode))
242 goto out_dir;
243
244 err = -ENOENT;
245 new_de = sysv_find_entry(new_dentry, &new_page);
246 if (!new_de)
247 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 sysv_set_link(new_de, new_page, old_inode);
249 new_inode->i_ctime = CURRENT_TIME_SEC;
250 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700251 drop_nlink(new_inode);
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800252 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 } else {
254 if (dir_de) {
255 err = -EMLINK;
256 if (new_dir->i_nlink >= SYSV_SB(new_dir->i_sb)->s_link_max)
257 goto out_dir;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 err = sysv_add_link(new_dentry, old_inode);
Al Viro4787d452011-03-02 09:38:45 -0500260 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto out_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (dir_de)
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800263 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
266 sysv_delete_entry(old_de, old_page);
Al Viro4787d452011-03-02 09:38:45 -0500267 mark_inode_dirty(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (dir_de) {
270 sysv_set_link(dir_de, dir_page, new_dir);
Alexey Dobriyan4e907c32006-03-23 03:00:52 -0800271 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 return 0;
274
275out_dir:
276 if (dir_de) {
277 kunmap(dir_page);
278 page_cache_release(dir_page);
279 }
280out_old:
281 kunmap(old_page);
282 page_cache_release(old_page);
283out:
284 return err;
285}
286
287/*
288 * directories can handle most operations...
289 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800290const struct inode_operations sysv_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 .create = sysv_create,
292 .lookup = sysv_lookup,
293 .link = sysv_link,
294 .unlink = sysv_unlink,
295 .symlink = sysv_symlink,
296 .mkdir = sysv_mkdir,
297 .rmdir = sysv_rmdir,
298 .mknod = sysv_mknod,
299 .rename = sysv_rename,
300 .getattr = sysv_getattr,
301};