blob: 9fe6d4ab74f01ef3d7055d4bb1ace9a73e441ad7 [file] [log] [blame]
Ryusuke Konishiae980432018-09-04 15:46:30 -07001// SPDX-License-Identifier: GPL-2.0+
Ryusuke Konishid2500652009-04-06 19:01:34 -07002/*
3 * namei.c - NILFS pathname lookup operations.
4 *
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -07007 * Modified for NILFS by Amagai Yoshiji and Ryusuke Konishi.
Ryusuke Konishid2500652009-04-06 19:01:34 -07008 */
9/*
10 * linux/fs/ext2/namei.c
11 *
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
16 *
17 * from
18 *
19 * linux/fs/minix/namei.c
20 *
21 * Copyright (C) 1991, 1992 Linus Torvalds
22 *
23 * Big-endian to little-endian byte-swapping/bitmaps by
24 * David S. Miller (davem@caip.rutgers.edu), 1995
25 */
26
27#include <linux/pagemap.h>
28#include "nilfs.h"
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +090029#include "export.h"
Ryusuke Konishid2500652009-04-06 19:01:34 -070030
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +090031#define NILFS_FID_SIZE_NON_CONNECTABLE \
32 (offsetof(struct nilfs_fid, parent_gen) / 4)
33#define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
Ryusuke Konishid2500652009-04-06 19:01:34 -070034
35static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
36{
37 int err = nilfs_add_link(dentry, inode);
Ryusuke Konishi4ad364c2016-05-23 16:23:25 -070038
Ryusuke Konishid2500652009-04-06 19:01:34 -070039 if (!err) {
Al Viro1e2e5472018-05-04 08:23:01 -040040 d_instantiate_new(dentry, inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -070041 return 0;
42 }
43 inode_dec_link_count(inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -080044 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -070045 iput(inode);
46 return err;
47}
48
49/*
50 * Methods themselves.
51 */
52
53static struct dentry *
Al Viro00cd8dd2012-06-10 17:13:09 -040054nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Ryusuke Konishid2500652009-04-06 19:01:34 -070055{
56 struct inode *inode;
57 ino_t ino;
58
59 if (dentry->d_name.len > NILFS_NAME_LEN)
60 return ERR_PTR(-ENAMETOOLONG);
61
Al Viro03190032010-01-31 21:02:09 -050062 ino = nilfs_inode_by_name(dir, &dentry->d_name);
Al Viroa9049372011-07-08 21:20:11 -040063 inode = ino ? nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino) : NULL;
Ryusuke Konishid2500652009-04-06 19:01:34 -070064 return d_splice_alias(inode, dentry);
65}
66
Ryusuke Konishid2500652009-04-06 19:01:34 -070067/*
68 * By the time this is called, we already have created
69 * the directory cache entry for the new file, but it
70 * is so far negative - it has no inode.
71 *
72 * If the create succeeds, we fill in the inode information
73 * with d_instantiate().
74 */
Al Viro4acdaf22011-07-26 01:42:34 -040075static int nilfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -040076 bool excl)
Ryusuke Konishid2500652009-04-06 19:01:34 -070077{
78 struct inode *inode;
79 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -070080 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -070081
82 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
83 if (err)
84 return err;
85 inode = nilfs_new_inode(dir, mode);
86 err = PTR_ERR(inode);
87 if (!IS_ERR(inode)) {
88 inode->i_op = &nilfs_file_inode_operations;
89 inode->i_fop = &nilfs_file_operations;
90 inode->i_mapping->a_ops = &nilfs_aops;
Jiro SEKIBAabdb3182009-11-27 19:41:14 +090091 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -070092 err = nilfs_add_nondir(dentry, inode);
93 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -070094 if (!err)
95 err = nilfs_transaction_commit(dir->i_sb);
96 else
97 nilfs_transaction_abort(dir->i_sb);
98
99 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700100}
101
102static int
Al Viro1a67aaf2011-07-26 01:52:52 -0400103nilfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700104{
105 struct inode *inode;
106 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700107 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700108
Ryusuke Konishid2500652009-04-06 19:01:34 -0700109 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
110 if (err)
111 return err;
112 inode = nilfs_new_inode(dir, mode);
113 err = PTR_ERR(inode);
114 if (!IS_ERR(inode)) {
115 init_special_inode(inode, inode->i_mode, rdev);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900116 nilfs_mark_inode_dirty(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700117 err = nilfs_add_nondir(dentry, inode);
118 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700119 if (!err)
120 err = nilfs_transaction_commit(dir->i_sb);
121 else
122 nilfs_transaction_abort(dir->i_sb);
123
124 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700125}
126
127static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
128 const char *symname)
129{
130 struct nilfs_transaction_info ti;
131 struct super_block *sb = dir->i_sb;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700132 unsigned int l = strlen(symname) + 1;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700133 struct inode *inode;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700134 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700135
136 if (l > sb->s_blocksize)
137 return -ENAMETOOLONG;
138
139 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
140 if (err)
141 return err;
142
Ryusuke Konishi3147db82017-11-17 15:29:46 -0800143 inode = nilfs_new_inode(dir, S_IFLNK | 0777);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700144 err = PTR_ERR(inode);
145 if (IS_ERR(inode))
146 goto out;
147
148 /* slow symlink */
149 inode->i_op = &nilfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500150 inode_nohighmem(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700151 inode->i_mapping->a_ops = &nilfs_aops;
152 err = page_symlink(inode, symname, l);
153 if (err)
154 goto out_fail;
155
156 /* mark_inode_dirty(inode); */
Jiro SEKIBA9ca941d2009-11-27 19:41:06 +0900157 /* page_symlink() do this */
Ryusuke Konishid2500652009-04-06 19:01:34 -0700158
159 err = nilfs_add_nondir(dentry, inode);
160out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700161 if (!err)
162 err = nilfs_transaction_commit(dir->i_sb);
163 else
164 nilfs_transaction_abort(dir->i_sb);
165
166 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700167
168out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900169 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900170 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800171 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700172 iput(inode);
173 goto out;
174}
175
176static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
177 struct dentry *dentry)
178{
David Howells2b0143b2015-03-17 22:25:59 +0000179 struct inode *inode = d_inode(old_dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700180 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700181 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700182
Ryusuke Konishid2500652009-04-06 19:01:34 -0700183 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
184 if (err)
185 return err;
186
Deepa Dinamani078cd822016-09-14 07:48:04 -0700187 inode->i_ctime = current_time(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700188 inode_inc_link_count(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400189 ihold(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700190
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800191 err = nilfs_add_link(dentry, inode);
192 if (!err) {
193 d_instantiate(dentry, inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700194 err = nilfs_transaction_commit(dir->i_sb);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800195 } else {
196 inode_dec_link_count(inode);
197 iput(inode);
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700198 nilfs_transaction_abort(dir->i_sb);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800199 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700200
201 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700202}
203
Al Viro18bb1db2011-07-26 01:41:39 -0400204static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700205{
206 struct inode *inode;
207 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700208 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700209
Ryusuke Konishid2500652009-04-06 19:01:34 -0700210 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
211 if (err)
212 return err;
213
Jiro SEKIBA565de402009-11-27 19:41:08 +0900214 inc_nlink(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700215
216 inode = nilfs_new_inode(dir, S_IFDIR | mode);
217 err = PTR_ERR(inode);
218 if (IS_ERR(inode))
219 goto out_dir;
220
221 inode->i_op = &nilfs_dir_inode_operations;
222 inode->i_fop = &nilfs_dir_operations;
223 inode->i_mapping->a_ops = &nilfs_aops;
224
Jiro SEKIBA565de402009-11-27 19:41:08 +0900225 inc_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700226
227 err = nilfs_make_empty(inode, dir);
228 if (err)
229 goto out_fail;
230
231 err = nilfs_add_link(dentry, inode);
232 if (err)
233 goto out_fail;
234
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900235 nilfs_mark_inode_dirty(inode);
Al Viro1e2e5472018-05-04 08:23:01 -0400236 d_instantiate_new(dentry, inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700237out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700238 if (!err)
239 err = nilfs_transaction_commit(dir->i_sb);
240 else
241 nilfs_transaction_abort(dir->i_sb);
242
243 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700244
245out_fail:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900246 drop_nlink(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900247 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900248 nilfs_mark_inode_dirty(inode);
Ryusuke Konishi705304a2014-12-10 15:54:34 -0800249 unlock_new_inode(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700250 iput(inode);
251out_dir:
Jiro SEKIBA565de402009-11-27 19:41:08 +0900252 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900253 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700254 goto out;
255}
256
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900257static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700258{
259 struct inode *inode;
260 struct nilfs_dir_entry *de;
261 struct page *page;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700262 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700263
Ryusuke Konishid2500652009-04-06 19:01:34 -0700264 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500265 de = nilfs_find_entry(dir, &dentry->d_name, &page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700266 if (!de)
267 goto out;
268
David Howells2b0143b2015-03-17 22:25:59 +0000269 inode = d_inode(dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700270 err = -EIO;
271 if (le64_to_cpu(de->inode) != inode->i_ino)
272 goto out;
273
274 if (!inode->i_nlink) {
Ryusuke Konishid6517de2016-08-02 14:05:14 -0700275 nilfs_msg(inode->i_sb, KERN_WARNING,
276 "deleting nonexistent file (ino=%lu), %d",
277 inode->i_ino, inode->i_nlink);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200278 set_nlink(inode, 1);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700279 }
280 err = nilfs_delete_entry(de, page);
281 if (err)
282 goto out;
283
284 inode->i_ctime = dir->i_ctime;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900285 drop_nlink(inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700286 err = 0;
287out:
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900288 return err;
289}
290
291static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
292{
293 struct nilfs_transaction_info ti;
294 int err;
295
296 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
297 if (err)
298 return err;
299
300 err = nilfs_do_unlink(dir, dentry);
301
302 if (!err) {
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900303 nilfs_mark_inode_dirty(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000304 nilfs_mark_inode_dirty(d_inode(dentry));
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700305 err = nilfs_transaction_commit(dir->i_sb);
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900306 } else
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700307 nilfs_transaction_abort(dir->i_sb);
308
309 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700310}
311
312static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
313{
David Howells2b0143b2015-03-17 22:25:59 +0000314 struct inode *inode = d_inode(dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700315 struct nilfs_transaction_info ti;
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700316 int err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700317
318 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
319 if (err)
320 return err;
321
322 err = -ENOTEMPTY;
323 if (nilfs_empty_dir(inode)) {
Jiro SEKIBA4cd76c32009-11-27 19:41:10 +0900324 err = nilfs_do_unlink(dir, dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700325 if (!err) {
326 inode->i_size = 0;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900327 drop_nlink(inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900328 nilfs_mark_inode_dirty(inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900329 drop_nlink(dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900330 nilfs_mark_inode_dirty(dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700331 }
332 }
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700333 if (!err)
334 err = nilfs_transaction_commit(dir->i_sb);
335 else
336 nilfs_transaction_abort(dir->i_sb);
337
338 return err;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700339}
340
341static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200342 struct inode *new_dir, struct dentry *new_dentry,
343 unsigned int flags)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700344{
David Howells2b0143b2015-03-17 22:25:59 +0000345 struct inode *old_inode = d_inode(old_dentry);
346 struct inode *new_inode = d_inode(new_dentry);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700347 struct page *dir_page = NULL;
348 struct nilfs_dir_entry *dir_de = NULL;
349 struct page *old_page;
350 struct nilfs_dir_entry *old_de;
351 struct nilfs_transaction_info ti;
352 int err;
353
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200354 if (flags & ~RENAME_NOREPLACE)
355 return -EINVAL;
356
Ryusuke Konishid2500652009-04-06 19:01:34 -0700357 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
358 if (unlikely(err))
359 return err;
360
361 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500362 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700363 if (!old_de)
364 goto out;
365
366 if (S_ISDIR(old_inode->i_mode)) {
367 err = -EIO;
368 dir_de = nilfs_dotdot(old_inode, &dir_page);
369 if (!dir_de)
370 goto out_old;
371 }
372
373 if (new_inode) {
374 struct page *new_page;
375 struct nilfs_dir_entry *new_de;
376
377 err = -ENOTEMPTY;
378 if (dir_de && !nilfs_empty_dir(new_inode))
379 goto out_dir;
380
381 err = -ENOENT;
Al Viro03190032010-01-31 21:02:09 -0500382 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700383 if (!new_de)
384 goto out_dir;
Ryusuke Konishid2500652009-04-06 19:01:34 -0700385 nilfs_set_link(new_dir, new_de, new_page, old_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900386 nilfs_mark_inode_dirty(new_dir);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700387 new_inode->i_ctime = current_time(new_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700388 if (dir_de)
389 drop_nlink(new_inode);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900390 drop_nlink(new_inode);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900391 nilfs_mark_inode_dirty(new_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700392 } else {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700393 err = nilfs_add_link(new_dentry, old_inode);
Al Viro30eb43d2011-03-02 12:01:13 -0500394 if (err)
Ryusuke Konishid2500652009-04-06 19:01:34 -0700395 goto out_dir;
Jiro SEKIBA565de402009-11-27 19:41:08 +0900396 if (dir_de) {
397 inc_nlink(new_dir);
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900398 nilfs_mark_inode_dirty(new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900399 }
Ryusuke Konishid2500652009-04-06 19:01:34 -0700400 }
401
402 /*
403 * Like most other Unix systems, set the ctime for inodes on a
404 * rename.
Ryusuke Konishid2500652009-04-06 19:01:34 -0700405 */
Deepa Dinamani078cd822016-09-14 07:48:04 -0700406 old_inode->i_ctime = current_time(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700407
408 nilfs_delete_entry(old_de, old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700409
410 if (dir_de) {
411 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
Jiro SEKIBA565de402009-11-27 19:41:08 +0900412 drop_nlink(old_dir);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700413 }
Jiro SEKIBAabdb3182009-11-27 19:41:14 +0900414 nilfs_mark_inode_dirty(old_dir);
415 nilfs_mark_inode_dirty(old_inode);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700416
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700417 err = nilfs_transaction_commit(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700418 return err;
419
420out_dir:
421 if (dir_de) {
422 kunmap(dir_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300423 put_page(dir_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700424 }
425out_old:
426 kunmap(old_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300427 put_page(old_page);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700428out:
Ryusuke Konishi47420c72009-04-06 19:01:45 -0700429 nilfs_transaction_abort(old_dir->i_sb);
Ryusuke Konishid2500652009-04-06 19:01:34 -0700430 return err;
431}
432
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900433/*
434 * Export operations
435 */
436static struct dentry *nilfs_get_parent(struct dentry *child)
437{
438 unsigned long ino;
439 struct inode *inode;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700440 struct qstr dotdot = QSTR_INIT("..", 2);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900441 struct nilfs_root *root;
442
David Howells2b0143b2015-03-17 22:25:59 +0000443 ino = nilfs_inode_by_name(d_inode(child), &dotdot);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900444 if (!ino)
445 return ERR_PTR(-ENOENT);
446
David Howells2b0143b2015-03-17 22:25:59 +0000447 root = NILFS_I(d_inode(child))->i_root;
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900448
Al Virofc640052016-04-10 01:33:30 -0400449 inode = nilfs_iget(child->d_sb, root, ino);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900450 if (IS_ERR(inode))
451 return ERR_CAST(inode);
452
453 return d_obtain_alias(inode);
454}
455
456static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
457 u64 ino, u32 gen)
458{
459 struct nilfs_root *root;
460 struct inode *inode;
461
462 if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
463 return ERR_PTR(-ESTALE);
464
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900465 root = nilfs_lookup_root(sb->s_fs_info, cno);
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900466 if (!root)
467 return ERR_PTR(-ESTALE);
468
469 inode = nilfs_iget(sb, root, ino);
470 nilfs_put_root(root);
471
472 if (IS_ERR(inode))
473 return ERR_CAST(inode);
474 if (gen && inode->i_generation != gen) {
475 iput(inode);
476 return ERR_PTR(-ESTALE);
477 }
478 return d_obtain_alias(inode);
479}
480
481static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
482 int fh_len, int fh_type)
483{
484 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
485
NeilBrownf73c2f12015-06-25 15:03:45 -0700486 if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900487 (fh_type != FILEID_NILFS_WITH_PARENT &&
488 fh_type != FILEID_NILFS_WITHOUT_PARENT))
489 return NULL;
490
491 return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
492}
493
494static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
495 int fh_len, int fh_type)
496{
497 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
498
NeilBrownf73c2f12015-06-25 15:03:45 -0700499 if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900500 fh_type != FILEID_NILFS_WITH_PARENT)
501 return NULL;
502
503 return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
504}
505
Al Virob0b03822012-04-02 14:34:06 -0400506static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
507 struct inode *parent)
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900508{
509 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900510 struct nilfs_root *root = NILFS_I(inode)->i_root;
511 int type;
512
Al Virob0b03822012-04-02 14:34:06 -0400513 if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
514 *lenp = NILFS_FID_SIZE_CONNECTABLE;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900515 return FILEID_INVALID;
Al Virob0b03822012-04-02 14:34:06 -0400516 }
517 if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
518 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
Namjae Jeon94e07a752013-02-17 15:48:11 +0900519 return FILEID_INVALID;
Al Virob0b03822012-04-02 14:34:06 -0400520 }
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900521
522 fid->cno = root->cno;
523 fid->ino = inode->i_ino;
524 fid->gen = inode->i_generation;
525
Al Virob0b03822012-04-02 14:34:06 -0400526 if (parent) {
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900527 fid->parent_ino = parent->i_ino;
528 fid->parent_gen = parent->i_generation;
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900529 type = FILEID_NILFS_WITH_PARENT;
530 *lenp = NILFS_FID_SIZE_CONNECTABLE;
531 } else {
532 type = FILEID_NILFS_WITHOUT_PARENT;
533 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
534 }
535
536 return type;
537}
538
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700539const struct inode_operations nilfs_dir_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700540 .create = nilfs_create,
541 .lookup = nilfs_lookup,
542 .link = nilfs_link,
543 .unlink = nilfs_unlink,
544 .symlink = nilfs_symlink,
545 .mkdir = nilfs_mkdir,
546 .rmdir = nilfs_rmdir,
547 .mknod = nilfs_mknod,
548 .rename = nilfs_rename,
549 .setattr = nilfs_setattr,
550 .permission = nilfs_permission,
Ryusuke Konishi622daaff2010-12-26 16:38:43 +0900551 .fiemap = nilfs_fiemap,
Ryusuke Konishid2500652009-04-06 19:01:34 -0700552};
553
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700554const struct inode_operations nilfs_special_inode_operations = {
Ryusuke Konishid2500652009-04-06 19:01:34 -0700555 .setattr = nilfs_setattr,
556 .permission = nilfs_permission,
557};
558
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700559const struct inode_operations nilfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -0500560 .get_link = page_get_link,
Ryusuke Konishidc3d3b82010-08-15 23:33:57 +0900561 .permission = nilfs_permission,
Ryusuke Konishid2500652009-04-06 19:01:34 -0700562};
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +0900563
564const struct export_operations nilfs_export_ops = {
565 .encode_fh = nilfs_encode_fh,
566 .fh_to_dentry = nilfs_fh_to_dentry,
567 .fh_to_parent = nilfs_fh_to_parent,
568 .get_parent = nilfs_get_parent,
569};