[SCSI] mpt2sas: modified _scsih_sas_device_find_by_handle/sas_address
[linux-2.6/linux-mips.git] / fs / nilfs2 / namei.c
blobad6ed2cf19b4b5d31f3b7a80cfe11377b5acfceb
1 /*
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)
31 * from
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>
42 #include "nilfs.h"
45 static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
47 int err = nilfs_add_link(dentry, inode);
48 if (!err) {
49 d_instantiate(dentry, inode);
50 return 0;
52 inode_dec_link_count(inode);
53 iput(inode);
54 return err;
58 * Methods themselves.
61 static struct dentry *
62 nilfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
64 struct inode *inode;
65 ino_t ino;
67 if (dentry->d_name.len > NILFS_NAME_LEN)
68 return ERR_PTR(-ENAMETOOLONG);
70 ino = nilfs_inode_by_name(dir, &dentry->d_name);
71 inode = NULL;
72 if (ino) {
73 inode = nilfs_iget(dir->i_sb, ino);
74 if (IS_ERR(inode))
75 return ERR_CAST(inode);
77 return d_splice_alias(inode, dentry);
80 struct dentry *nilfs_get_parent(struct dentry *child)
82 unsigned long ino;
83 struct inode *inode;
84 struct qstr dotdot = {.name = "..", .len = 2};
86 ino = nilfs_inode_by_name(child->d_inode, &dotdot);
87 if (!ino)
88 return ERR_PTR(-ENOENT);
90 inode = nilfs_iget(child->d_inode->i_sb, ino);
91 if (IS_ERR(inode))
92 return ERR_CAST(inode);
93 return d_obtain_alias(inode);
97 * By the time this is called, we already have created
98 * the directory cache entry for the new file, but it
99 * is so far negative - it has no inode.
101 * If the create succeeds, we fill in the inode information
102 * with d_instantiate().
104 static int nilfs_create(struct inode *dir, struct dentry *dentry, int mode,
105 struct nameidata *nd)
107 struct inode *inode;
108 struct nilfs_transaction_info ti;
109 int err;
111 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
112 if (err)
113 return err;
114 inode = nilfs_new_inode(dir, mode);
115 err = PTR_ERR(inode);
116 if (!IS_ERR(inode)) {
117 inode->i_op = &nilfs_file_inode_operations;
118 inode->i_fop = &nilfs_file_operations;
119 inode->i_mapping->a_ops = &nilfs_aops;
120 nilfs_mark_inode_dirty(inode);
121 err = nilfs_add_nondir(dentry, inode);
123 if (!err)
124 err = nilfs_transaction_commit(dir->i_sb);
125 else
126 nilfs_transaction_abort(dir->i_sb);
128 return err;
131 static int
132 nilfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
134 struct inode *inode;
135 struct nilfs_transaction_info ti;
136 int err;
138 if (!new_valid_dev(rdev))
139 return -EINVAL;
141 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
142 if (err)
143 return err;
144 inode = nilfs_new_inode(dir, mode);
145 err = PTR_ERR(inode);
146 if (!IS_ERR(inode)) {
147 init_special_inode(inode, inode->i_mode, rdev);
148 nilfs_mark_inode_dirty(inode);
149 err = nilfs_add_nondir(dentry, inode);
151 if (!err)
152 err = nilfs_transaction_commit(dir->i_sb);
153 else
154 nilfs_transaction_abort(dir->i_sb);
156 return err;
159 static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
160 const char *symname)
162 struct nilfs_transaction_info ti;
163 struct super_block *sb = dir->i_sb;
164 unsigned l = strlen(symname)+1;
165 struct inode *inode;
166 int err;
168 if (l > sb->s_blocksize)
169 return -ENAMETOOLONG;
171 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
172 if (err)
173 return err;
175 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
176 err = PTR_ERR(inode);
177 if (IS_ERR(inode))
178 goto out;
180 /* slow symlink */
181 inode->i_op = &nilfs_symlink_inode_operations;
182 inode->i_mapping->a_ops = &nilfs_aops;
183 err = page_symlink(inode, symname, l);
184 if (err)
185 goto out_fail;
187 /* mark_inode_dirty(inode); */
188 /* page_symlink() do this */
190 err = nilfs_add_nondir(dentry, inode);
191 out:
192 if (!err)
193 err = nilfs_transaction_commit(dir->i_sb);
194 else
195 nilfs_transaction_abort(dir->i_sb);
197 return err;
199 out_fail:
200 drop_nlink(inode);
201 nilfs_mark_inode_dirty(inode);
202 iput(inode);
203 goto out;
206 static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
207 struct dentry *dentry)
209 struct inode *inode = old_dentry->d_inode;
210 struct nilfs_transaction_info ti;
211 int err;
213 if (inode->i_nlink >= NILFS_LINK_MAX)
214 return -EMLINK;
216 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
217 if (err)
218 return err;
220 inode->i_ctime = CURRENT_TIME;
221 inode_inc_link_count(inode);
222 atomic_inc(&inode->i_count);
224 err = nilfs_add_nondir(dentry, inode);
225 if (!err)
226 err = nilfs_transaction_commit(dir->i_sb);
227 else
228 nilfs_transaction_abort(dir->i_sb);
230 return err;
233 static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
235 struct inode *inode;
236 struct nilfs_transaction_info ti;
237 int err;
239 if (dir->i_nlink >= NILFS_LINK_MAX)
240 return -EMLINK;
242 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
243 if (err)
244 return err;
246 inc_nlink(dir);
248 inode = nilfs_new_inode(dir, S_IFDIR | mode);
249 err = PTR_ERR(inode);
250 if (IS_ERR(inode))
251 goto out_dir;
253 inode->i_op = &nilfs_dir_inode_operations;
254 inode->i_fop = &nilfs_dir_operations;
255 inode->i_mapping->a_ops = &nilfs_aops;
257 inc_nlink(inode);
259 err = nilfs_make_empty(inode, dir);
260 if (err)
261 goto out_fail;
263 err = nilfs_add_link(dentry, inode);
264 if (err)
265 goto out_fail;
267 nilfs_mark_inode_dirty(inode);
268 d_instantiate(dentry, inode);
269 out:
270 if (!err)
271 err = nilfs_transaction_commit(dir->i_sb);
272 else
273 nilfs_transaction_abort(dir->i_sb);
275 return err;
277 out_fail:
278 drop_nlink(inode);
279 drop_nlink(inode);
280 nilfs_mark_inode_dirty(inode);
281 iput(inode);
282 out_dir:
283 drop_nlink(dir);
284 nilfs_mark_inode_dirty(dir);
285 goto out;
288 static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
290 struct inode *inode;
291 struct nilfs_dir_entry *de;
292 struct page *page;
293 int err;
295 err = -ENOENT;
296 de = nilfs_find_entry(dir, &dentry->d_name, &page);
297 if (!de)
298 goto out;
300 inode = dentry->d_inode;
301 err = -EIO;
302 if (le64_to_cpu(de->inode) != inode->i_ino)
303 goto out;
305 if (!inode->i_nlink) {
306 nilfs_warning(inode->i_sb, __func__,
307 "deleting nonexistent file (%lu), %d\n",
308 inode->i_ino, inode->i_nlink);
309 inode->i_nlink = 1;
311 err = nilfs_delete_entry(de, page);
312 if (err)
313 goto out;
315 inode->i_ctime = dir->i_ctime;
316 drop_nlink(inode);
317 err = 0;
318 out:
319 return err;
322 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
324 struct nilfs_transaction_info ti;
325 int err;
327 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
328 if (err)
329 return err;
331 err = nilfs_do_unlink(dir, dentry);
333 if (!err) {
334 nilfs_mark_inode_dirty(dir);
335 nilfs_mark_inode_dirty(dentry->d_inode);
336 err = nilfs_transaction_commit(dir->i_sb);
337 } else
338 nilfs_transaction_abort(dir->i_sb);
340 return err;
343 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
345 struct inode *inode = dentry->d_inode;
346 struct nilfs_transaction_info ti;
347 int err;
349 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
350 if (err)
351 return err;
353 err = -ENOTEMPTY;
354 if (nilfs_empty_dir(inode)) {
355 err = nilfs_do_unlink(dir, dentry);
356 if (!err) {
357 inode->i_size = 0;
358 drop_nlink(inode);
359 nilfs_mark_inode_dirty(inode);
360 drop_nlink(dir);
361 nilfs_mark_inode_dirty(dir);
364 if (!err)
365 err = nilfs_transaction_commit(dir->i_sb);
366 else
367 nilfs_transaction_abort(dir->i_sb);
369 return err;
372 static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
373 struct inode *new_dir, struct dentry *new_dentry)
375 struct inode *old_inode = old_dentry->d_inode;
376 struct inode *new_inode = new_dentry->d_inode;
377 struct page *dir_page = NULL;
378 struct nilfs_dir_entry *dir_de = NULL;
379 struct page *old_page;
380 struct nilfs_dir_entry *old_de;
381 struct nilfs_transaction_info ti;
382 int err;
384 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
385 if (unlikely(err))
386 return err;
388 err = -ENOENT;
389 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
390 if (!old_de)
391 goto out;
393 if (S_ISDIR(old_inode->i_mode)) {
394 err = -EIO;
395 dir_de = nilfs_dotdot(old_inode, &dir_page);
396 if (!dir_de)
397 goto out_old;
400 if (new_inode) {
401 struct page *new_page;
402 struct nilfs_dir_entry *new_de;
404 err = -ENOTEMPTY;
405 if (dir_de && !nilfs_empty_dir(new_inode))
406 goto out_dir;
408 err = -ENOENT;
409 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
410 if (!new_de)
411 goto out_dir;
412 inc_nlink(old_inode);
413 nilfs_set_link(new_dir, new_de, new_page, old_inode);
414 nilfs_mark_inode_dirty(new_dir);
415 new_inode->i_ctime = CURRENT_TIME;
416 if (dir_de)
417 drop_nlink(new_inode);
418 drop_nlink(new_inode);
419 nilfs_mark_inode_dirty(new_inode);
420 } else {
421 if (dir_de) {
422 err = -EMLINK;
423 if (new_dir->i_nlink >= NILFS_LINK_MAX)
424 goto out_dir;
426 inc_nlink(old_inode);
427 err = nilfs_add_link(new_dentry, old_inode);
428 if (err) {
429 drop_nlink(old_inode);
430 nilfs_mark_inode_dirty(old_inode);
431 goto out_dir;
433 if (dir_de) {
434 inc_nlink(new_dir);
435 nilfs_mark_inode_dirty(new_dir);
440 * Like most other Unix systems, set the ctime for inodes on a
441 * rename.
443 old_inode->i_ctime = CURRENT_TIME;
445 nilfs_delete_entry(old_de, old_page);
446 drop_nlink(old_inode);
448 if (dir_de) {
449 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
450 drop_nlink(old_dir);
452 nilfs_mark_inode_dirty(old_dir);
453 nilfs_mark_inode_dirty(old_inode);
455 err = nilfs_transaction_commit(old_dir->i_sb);
456 return err;
458 out_dir:
459 if (dir_de) {
460 kunmap(dir_page);
461 page_cache_release(dir_page);
463 out_old:
464 kunmap(old_page);
465 page_cache_release(old_page);
466 out:
467 nilfs_transaction_abort(old_dir->i_sb);
468 return err;
471 const struct inode_operations nilfs_dir_inode_operations = {
472 .create = nilfs_create,
473 .lookup = nilfs_lookup,
474 .link = nilfs_link,
475 .unlink = nilfs_unlink,
476 .symlink = nilfs_symlink,
477 .mkdir = nilfs_mkdir,
478 .rmdir = nilfs_rmdir,
479 .mknod = nilfs_mknod,
480 .rename = nilfs_rename,
481 .setattr = nilfs_setattr,
482 .permission = nilfs_permission,
485 const struct inode_operations nilfs_special_inode_operations = {
486 .setattr = nilfs_setattr,
487 .permission = nilfs_permission,
490 const struct inode_operations nilfs_symlink_inode_operations = {
491 .readlink = generic_readlink,
492 .follow_link = page_follow_link_light,
493 .put_link = page_put_link,