blob: 7ccdb961eea90d2ed313ef688feca1451112f5ba [file] [log] [blame]
Ryusuke Konishid2500652009-04-06 19:01:34 -07001/*
2 * namei.c - NILFS pathname lookup operations.
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
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.
10 *
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.
15 *
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
19 *
20 * Modified for NILFS by Amagai Yoshiji <amagai@osrg.net>,
21 * Ryusuke Konishi <ryusuke@osrg.net>
22 */
23/*
24 * linux/fs/ext2/namei.c
25 *
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)
30 *
31 * from
32 *
33 * linux/fs/minix/namei.c
34 *
35 * Copyright (C) 1991, 1992 Linus Torvalds
36 *
37 * Big-endian to little-endian byte-swapping/bitmaps by
38 * David S. Miller (davem@caip.rutgers.edu), 1995
39 */
40
41#include <linux/pagemap.h>
42#include "nilfs.h"
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +090043#include "export.h"
Ryusuke Konishid2500652009-04-06 19:01:34 -070044
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +090045#define NILFS_FID_SIZE_NON_CONNECTABLE \
46 (offsetof(struct nilfs_fid, parent_gen) / 4)
47#define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
Ryusuke Konishid2500652009-04-06 19:01:34 -070048
49static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
50{
51 int err = nilfs_add_link(dentry, inode);
52 if (!err) {
53 d_instantiate(dentry, inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -080054 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -070055 return 0;
56 }
57 inode_dec_link_count(inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -080058 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -070059 iput(inode);
60 return err;
61}
62
63/*
64 * Methods themselves.
65 */
66
67static struct dentry *
Al Viro00cd8dd2012-06-10 17:13:09 -040068nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Ryusuke Konishid2500652009-04-06 19:01:34 -070069{
70 struct inode *inode;
71 ino_t ino;
72
73 if (dentry->d_name.len > NILFS_NAME_LEN)
74 return ERR_PTR(-ENAMETOOLONG);
75
Al Viro03190032010-01-31 21:02:09 -050076 ino = nilfs_inode_by_name(dir, &dentry->d_name);
Al Viroa9049372011-07-08 21:20:11 -040077 inode = ino ? nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino) : NULL;
Ryusuke Konishid2500652009-04-06 19:01:34 -070078 return d_splice_alias(inode, dentry);
79}
80
Ryusuke Konishid2500652009-04-06 19:01:34 -070081/*
82 * By the time this is called, we already have created
83 * the directory cache entry for the new file, but it
84 * is so far negative - it has no inode.
85 *
86 * If the create succeeds, we fill in the inode information
87 * with d_instantiate().
88 */
Al Viro4acdaf22011-07-26 01:42:34 -040089static int nilfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040090 bool excl)
Ryusuke Konishid2500652009-04-06 19:01:34 -070091{
92 struct inode *inode;
93 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -070094 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -070095
96 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
97 if (err)
98 return err;
99 inode = nilfs_new_inode(dir, mode);
100 err = PTR_ERR(inode);
101 if (!IS_ERR(inode)) {
102 inode->i_op = &nilfs_file_inode_operations;
103 inode->i_fop = &nilfs_file_operations;
104 inode->i_mapping->a_ops = &nilfs_aops;
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900105 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700106 err = nilfs_add_nondir(dentry, inode);
107 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700108 if (!err)
109 err = nilfs_transaction_commit(dir->i_sb);
110 else
111 nilfs_transaction_abort(dir->i_sb);
112
113 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700114}
115
116static int
Al Viro1a67aaf2011-07-26 01:52:52 -0400117nilfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700118{
119 struct inode *inode;
120 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700121 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700122
Ryusuke Konishid2500652009-04-06 19:01:34 -0700123 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
124 if (err)
125 return err;
126 inode = nilfs_new_inode(dir, mode);
127 err = PTR_ERR(inode);
128 if (!IS_ERR(inode)) {
129 init_special_inode(inode, inode->i_mode, rdev);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900130 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700131 err = nilfs_add_nondir(dentry, inode);
132 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700133 if (!err)
134 err = nilfs_transaction_commit(dir->i_sb);
135 else
136 nilfs_transaction_abort(dir->i_sb);
137
138 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700139}
140
141static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
142 const char *symname)
143{
144 struct nilfs_transaction_info ti;
145 struct super_block *sb = dir->i_sb;
146 unsigned l = strlen(symname)+1;
147 struct inode *inode;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700148 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700149
150 if (l > sb->s_blocksize)
151 return -ENAMETOOLONG;
152
153 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
154 if (err)
155 return err;
156
157 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
158 err = PTR_ERR(inode);
159 if (IS_ERR(inode))
160 goto out;
161
162 /* slow symlink */
163 inode->i_op = &nilfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500164 inode_nohighmem(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700165 inode->i_mapping->a_ops = &nilfs_aops;
166 err = page_symlink(inode, symname, l);
167 if (err)
168 goto out_fail;
169
170 /* mark_inode_dirty(inode); */
Jiro SEKIBA9ca941d2009-11-27 19:41:06 +0900171 /* page_symlink() do this */
Ryusuke Konishid2500652009-04-06 19:01:34 -0700172
173 err = nilfs_add_nondir(dentry, inode);
174out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700175 if (!err)
176 err = nilfs_transaction_commit(dir->i_sb);
177 else
178 nilfs_transaction_abort(dir->i_sb);
179
180 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700181
182out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900183 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900184 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800185 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700186 iput(inode);
187 goto out;
188}
189
190static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
191 struct dentry *dentry)
192{
David Howells2b0143b2015-03-17 22:25:59 +0000193 struct inode *inode = d_inode(old_dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700194 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700195 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700196
Ryusuke Konishid2500652009-04-06 19:01:34 -0700197 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
198 if (err)
199 return err;
200
201 inode->i_ctime = CURRENT_TIME;
202 inode_inc_link_count(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400203 ihold(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700204
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800205 err = nilfs_add_link(dentry, inode);
206 if (!err) {
207 d_instantiate(dentry, inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700208 err = nilfs_transaction_commit(dir->i_sb);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800209 } else {
210 inode_dec_link_count(inode);
211 iput(inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700212 nilfs_transaction_abort(dir->i_sb);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800213 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700214
215 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700216}
217
Al Viro18bb1db2011-07-26 01:41:39 -0400218static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700219{
220 struct inode *inode;
221 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700222 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700223
Ryusuke Konishid2500652009-04-06 19:01:34 -0700224 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
225 if (err)
226 return err;
227
Jiro SEKIBA565de402009-11-27 19:41:08 +0900228 inc_nlink(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700229
230 inode = nilfs_new_inode(dir, S_IFDIR | mode);
231 err = PTR_ERR(inode);
232 if (IS_ERR(inode))
233 goto out_dir;
234
235 inode->i_op = &nilfs_dir_inode_operations;
236 inode->i_fop = &nilfs_dir_operations;
237 inode->i_mapping->a_ops = &nilfs_aops;
238
Jiro SEKIBA565de402009-11-27 19:41:08 +0900239 inc_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700240
241 err = nilfs_make_empty(inode, dir);
242 if (err)
243 goto out_fail;
244
245 err = nilfs_add_link(dentry, inode);
246 if (err)
247 goto out_fail;
248
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900249 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700250 d_instantiate(dentry, inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800251 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700252out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700253 if (!err)
254 err = nilfs_transaction_commit(dir->i_sb);
255 else
256 nilfs_transaction_abort(dir->i_sb);
257
258 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700259
260out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900261 drop_nlink(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900262 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900263 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800264 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700265 iput(inode);
266out_dir:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900267 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900268 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700269 goto out;
270}
271
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900272static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700273{
274 struct inode *inode;
275 struct nilfs_dir_entry *de;
276 struct page *page;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700277 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700278
Ryusuke Konishid2500652009-04-06 19:01:34 -0700279 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500280 de = nilfs_find_entry(dir, &dentry->d_name, &page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700281 if (!de)
282 goto out;
283
David Howells2b0143b2015-03-17 22:25:59 +0000284 inode = d_inode(dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700285 err = -EIO;
286 if (le64_to_cpu(de->inode) != inode->i_ino)
287 goto out;
288
289 if (!inode->i_nlink) {
290 nilfs_warning(inode->i_sb, __func__,
291 "deleting nonexistent file (%lu), %d\n",
292 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200293 set_nlink(inode, 1);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700294 }
295 err = nilfs_delete_entry(de, page);
296 if (err)
297 goto out;
298
299 inode->i_ctime = dir->i_ctime;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900300 drop_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700301 err = 0;
302out:
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900303 return err;
304}
305
306static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
307{
308 struct nilfs_transaction_info ti;
309 int err;
310
311 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
312 if (err)
313 return err;
314
315 err = nilfs_do_unlink(dir, dentry);
316
317 if (!err) {
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900318 nilfs_mark_inode_dirty(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000319 nilfs_mark_inode_dirty(d_inode(dentry));
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700320 err = nilfs_transaction_commit(dir->i_sb);
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900321 } else
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700322 nilfs_transaction_abort(dir->i_sb);
323
324 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700325}
326
327static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
328{
David Howells2b0143b2015-03-17 22:25:59 +0000329 struct inode *inode = d_inode(dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700330 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700331 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700332
333 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
334 if (err)
335 return err;
336
337 err = -ENOTEMPTY;
338 if (nilfs_empty_dir(inode)) {
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900339 err = nilfs_do_unlink(dir, dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700340 if (!err) {
341 inode->i_size = 0;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900342 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900343 nilfs_mark_inode_dirty(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900344 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900345 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700346 }
347 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700348 if (!err)
349 err = nilfs_transaction_commit(dir->i_sb);
350 else
351 nilfs_transaction_abort(dir->i_sb);
352
353 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700354}
355
356static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
357 struct inode *new_dir, struct dentry *new_dentry)
358{
David Howells2b0143b2015-03-17 22:25:59 +0000359 struct inode *old_inode = d_inode(old_dentry);
360 struct inode *new_inode = d_inode(new_dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700361 struct page *dir_page = NULL;
362 struct nilfs_dir_entry *dir_de = NULL;
363 struct page *old_page;
364 struct nilfs_dir_entry *old_de;
365 struct nilfs_transaction_info ti;
366 int err;
367
368 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
369 if (unlikely(err))
370 return err;
371
372 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500373 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700374 if (!old_de)
375 goto out;
376
377 if (S_ISDIR(old_inode->i_mode)) {
378 err = -EIO;
379 dir_de = nilfs_dotdot(old_inode, &dir_page);
380 if (!dir_de)
381 goto out_old;
382 }
383
384 if (new_inode) {
385 struct page *new_page;
386 struct nilfs_dir_entry *new_de;
387
388 err = -ENOTEMPTY;
389 if (dir_de && !nilfs_empty_dir(new_inode))
390 goto out_dir;
391
392 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500393 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700394 if (!new_de)
395 goto out_dir;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700396 nilfs_set_link(new_dir, new_de, new_page, old_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900397 nilfs_mark_inode_dirty(new_dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700398 new_inode->i_ctime = CURRENT_TIME;
399 if (dir_de)
400 drop_nlink(new_inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900401 drop_nlink(new_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900402 nilfs_mark_inode_dirty(new_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700403 } else {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700404 err = nilfs_add_link(new_dentry, old_inode);
Al Viro30eb43d2011-03-02 12:01:13 -0500405 if (err)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700406 goto out_dir;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900407 if (dir_de) {
408 inc_nlink(new_dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900409 nilfs_mark_inode_dirty(new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900410 }
Ryusuke Konishid2500652009-04-06 19:01:34 -0700411 }
412
413 /*
414 * Like most other Unix systems, set the ctime for inodes on a
415 * rename.
Ryusuke Konishid2500652009-04-06 19:01:34 -0700416 */
417 old_inode->i_ctime = CURRENT_TIME;
418
419 nilfs_delete_entry(old_de, old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700420
421 if (dir_de) {
422 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900423 drop_nlink(old_dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700424 }
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900425 nilfs_mark_inode_dirty(old_dir);
426 nilfs_mark_inode_dirty(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700427
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700428 err = nilfs_transaction_commit(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700429 return err;
430
431out_dir:
432 if (dir_de) {
433 kunmap(dir_page);
434 page_cache_release(dir_page);
435 }
436out_old:
437 kunmap(old_page);
438 page_cache_release(old_page);
439out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700440 nilfs_transaction_abort(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700441 return err;
442}
443
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900444/*
445 * Export operations
446 */
447static struct dentry *nilfs_get_parent(struct dentry *child)
448{
449 unsigned long ino;
450 struct inode *inode;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700451 struct qstr dotdot = QSTR_INIT("..", 2);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900452 struct nilfs_root *root;
453
David Howells2b0143b2015-03-17 22:25:59 +0000454 ino = nilfs_inode_by_name(d_inode(child), &dotdot);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900455 if (!ino)
456 return ERR_PTR(-ENOENT);
457
David Howells2b0143b2015-03-17 22:25:59 +0000458 root = NILFS_I(d_inode(child))->i_root;
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900459
David Howells2b0143b2015-03-17 22:25:59 +0000460 inode = nilfs_iget(d_inode(child)->i_sb, root, ino);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900461 if (IS_ERR(inode))
462 return ERR_CAST(inode);
463
464 return d_obtain_alias(inode);
465}
466
467static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
468 u64 ino, u32 gen)
469{
470 struct nilfs_root *root;
471 struct inode *inode;
472
473 if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
474 return ERR_PTR(-ESTALE);
475
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900476 root = nilfs_lookup_root(sb->s_fs_info, cno);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900477 if (!root)
478 return ERR_PTR(-ESTALE);
479
480 inode = nilfs_iget(sb, root, ino);
481 nilfs_put_root(root);
482
483 if (IS_ERR(inode))
484 return ERR_CAST(inode);
485 if (gen && inode->i_generation != gen) {
486 iput(inode);
487 return ERR_PTR(-ESTALE);
488 }
489 return d_obtain_alias(inode);
490}
491
492static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
493 int fh_len, int fh_type)
494{
495 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
496
NeilBrownf73c2f12015-06-25 15:03:45 -0700497 if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900498 (fh_type != FILEID_NILFS_WITH_PARENT &&
499 fh_type != FILEID_NILFS_WITHOUT_PARENT))
500 return NULL;
501
502 return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
503}
504
505static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
506 int fh_len, int fh_type)
507{
508 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
509
NeilBrownf73c2f12015-06-25 15:03:45 -0700510 if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900511 fh_type != FILEID_NILFS_WITH_PARENT)
512 return NULL;
513
514 return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
515}
516
Al Virob0b03822012-04-02 14:34:06 -0400517static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
518 struct inode *parent)
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900519{
520 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900521 struct nilfs_root *root = NILFS_I(inode)->i_root;
522 int type;
523
Al Virob0b03822012-04-02 14:34:06 -0400524 if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
525 *lenp = NILFS_FID_SIZE_CONNECTABLE;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900526 return FILEID_INVALID;
Al Virob0b03822012-04-02 14:34:06 -0400527 }
528 if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
529 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900530 return FILEID_INVALID;
Al Virob0b03822012-04-02 14:34:06 -0400531 }
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900532
533 fid->cno = root->cno;
534 fid->ino = inode->i_ino;
535 fid->gen = inode->i_generation;
536
Al Virob0b03822012-04-02 14:34:06 -0400537 if (parent) {
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900538 fid->parent_ino = parent->i_ino;
539 fid->parent_gen = parent->i_generation;
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900540 type = FILEID_NILFS_WITH_PARENT;
541 *lenp = NILFS_FID_SIZE_CONNECTABLE;
542 } else {
543 type = FILEID_NILFS_WITHOUT_PARENT;
544 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
545 }
546
547 return type;
548}
549
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700550const struct inode_operations nilfs_dir_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700551 .create = nilfs_create,
552 .lookup = nilfs_lookup,
553 .link = nilfs_link,
554 .unlink = nilfs_unlink,
555 .symlink = nilfs_symlink,
556 .mkdir = nilfs_mkdir,
557 .rmdir = nilfs_rmdir,
558 .mknod = nilfs_mknod,
559 .rename = nilfs_rename,
560 .setattr = nilfs_setattr,
561 .permission = nilfs_permission,
Ryusuke Konishi622daaff2010-12-26 16:38:43 +0900562 .fiemap = nilfs_fiemap,
Ryusuke Konishid2500652009-04-06 19:01:34 -0700563};
564
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700565const struct inode_operations nilfs_special_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700566 .setattr = nilfs_setattr,
567 .permission = nilfs_permission,
568};
569
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700570const struct inode_operations nilfs_symlink_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700571 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -0500572 .get_link = page_get_link,
Ryusuke Konishidc3d3b82010-08-15 23:33:57 +0900573 .permission = nilfs_permission,
Ryusuke Konishid2500652009-04-06 19:01:34 -0700574};
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900575
576const struct export_operations nilfs_export_ops = {
577 .encode_fh = nilfs_encode_fh,
578 .fh_to_dentry = nilfs_fh_to_dentry,
579 .fh_to_parent = nilfs_fh_to_parent,
580 .get_parent = nilfs_get_parent,
581};